diff --git a/404.html b/404.html index cce25f0..03b9f2e 100644 --- a/404.html +++ b/404.html @@ -21,11 +21,11 @@ } - +
- + diff --git a/assets/js/1.83b28654.js b/assets/js/1.546b57fd.js similarity index 96% rename from assets/js/1.83b28654.js rename to assets/js/1.546b57fd.js index beb8c92..226c57a 100644 --- a/assets/js/1.83b28654.js +++ b/assets/js/1.546b57fd.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[1,12,14,21,22,27],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return s})),n.d(e,"a",(function(){return r})),n.d(e,"i",(function(){return o})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return h})),n.d(e,"e",(function(){return f})),n.d(e,"k",(function(){return p})),n.d(e,"l",(function(){return d})),n.d(e,"c",(function(){return m})),n.d(e,"j",(function(){return k}));n(91);const s=/#.*$/,i=/\.(md|html)$/,r=/\/$/,o=/^[a-z]+:/i;function a(t){return decodeURI(t).replace(s,"").replace(i,"")}function l(t){return o.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function h(t){if(l(t))return t;const e=t.match(s),n=e?e[0]:"",i=a(t);return r.test(i)?t:i+".html"+n}function f(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(s);if(e)return e[0]}(e);if(i&&n!==i)return!1;return a(t.path)===a(e)}function p(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const s=t.charAt(0);if("/"===s)return t;if("?"===s||"#"===s)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const r=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,s,i=1){if("string"==typeof e)return p(n,e,s);if(Array.isArray(e))return Object.assign(p(n,e[0],s),{title:e[1]});{const r=e.children||[];return 0===r.length&&e.path?Object.assign(p(n,e.path,s),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:r.map(e=>t(e,n,s,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function g(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function k(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var s=n(242),i={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(s.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(s.g)(this.link)||Object(s.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(s.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(s.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},r=n(10),o=Object(r.a)(i,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=o.exports},245:function(t,e,n){"use strict";n.r(e);var s={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},i=(n(246),n(10)),r=Object(i.a)(s,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=r.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},250:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},259:function(t,e,n){"use strict";n.r(e);var s=n(244),i=n(245),r=n(92),o=n.n(r),a={name:"DropdownLink",components:{NavLink:s.default,DropdownTransition:i.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>o()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(a,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,s){return e("li",{key:n.link||s,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(s){return e("li",{key:s.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:s},on:{focusout:function(e){t.isLastItemOfArray(s,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},262:function(t,e,n){"use strict";n(250)},263:function(t,e,n){},270:function(t,e,n){"use strict";n.r(e);var s=n(259),i=n(242),r={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:s.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,s=this.$site.themeConfig.locales||{},i={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(i=>{const r=t[i],o=s[i]&&s[i].label||r.lang;let a;return r.lang===this.$lang?a=e:(a=e.replace(this.$localeConfig.path,i),n.some(t=>t.path===a)||(a=i)),{text:o,link:a}})};return[...this.userNav,i]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(i.j)(t),{items:(t.items||[]).map(i.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;n{let s=i()(e,"title","");return i()(e,"frontmatter.tags")&&(s+=" "+e.frontmatter.tags.join(" ")),n&&(s+=" "+n),o(t,s)};const o=(t,e)=>{const n=t=>t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"),s=new RegExp("[^\0-]"),i=t.split(/\s+/g).map(t=>t.trim()).filter(t=>!!t);if(s.test(t))return i.some(t=>e.toLowerCase().indexOf(t)>-1);{const s=t.endsWith(" ");return new RegExp(i.map((t,e)=>i.length!==e+1||s?`(?=.*\\b${n(t)}\\b)`:`(?=.*\\b${n(t)})`).join("")+".+","gi").test(e)}};var a={name:"SearchBox",data:()=>({query:"",focused:!1,focusIndex:0,placeholder:void 0}),computed:{showSuggestions(){return this.focused&&this.suggestions&&this.suggestions.length},suggestions(){const t=this.query.trim().toLowerCase();if(!t)return;const{pages:e}=this.$site,n=this.$site.themeConfig.searchMaxSuggestions||5,s=this.$localePath,i=[];for(let o=0;o=n);o++){const a=e[o];if(this.getPageLocalePath(a)===s&&this.isSearchable(a))if(r(t,a))i.push(a);else if(a.headers)for(let e=0;e=n);e++){const n=a.headers[e];n.title&&r(t,a,n.title)&&i.push(Object.assign({},a,{path:a.path+"#"+n.slug,header:n}))}}return i},alignRight(){return(this.$site.themeConfig.nav||[]).length+(this.$site.repo?1:0)<=2}},mounted(){this.placeholder=this.$site.themeConfig.searchPlaceholder||"",document.addEventListener("keydown",this.onHotkey)},beforeDestroy(){document.removeEventListener("keydown",this.onHotkey)},methods:{getPageLocalePath(t){for(const e in this.$site.locales||{})if("/"!==e&&0===t.path.indexOf(e))return e;return"/"},isSearchable(t){let e=null;return null===e||(e=Array.isArray(e)?e:new Array(e),e.filter(e=>t.path.match(e)).length>0)},onHotkey(t){t.srcElement===document.body&&["s","/"].includes(t.key)&&(this.$refs.input.focus(),t.preventDefault())},onUp(){this.showSuggestions&&(this.focusIndex>0?this.focusIndex--:this.focusIndex=this.suggestions.length-1)},onDown(){this.showSuggestions&&(this.focusIndex "+t._s(n.header.title))]):t._e()])])})),0):t._e()])}),[],!1,null,null,null).exports,c=n(291),h=n(270);function f(t,e){return t.ownerDocument.defaultView.getComputedStyle(t,null)[e]}var p={name:"Navbar",components:{SidebarButton:c.default,NavLinks:h.default,SearchBox:u,AlgoliaSearchBox:{}},data:()=>({linksWrapMaxWidth:null}),computed:{algolia(){return this.$themeLocaleConfig.algolia||this.$site.themeConfig.algolia||{}},isAlgoliaSearch(){return this.algolia&&this.algolia.apiKey&&this.algolia.indexName}},mounted(){const t=parseInt(f(this.$el,"paddingLeft"))+parseInt(f(this.$el,"paddingRight")),e=()=>{document.documentElement.clientWidth<719?this.linksWrapMaxWidth=null:this.linksWrapMaxWidth=this.$el.offsetWidth-t-(this.$refs.siteName&&this.$refs.siteName.offsetWidth||0)};e(),window.addEventListener("resize",e,!1)}},d=(n(298),Object(l.a)(p,(function(){var t=this,e=t._self._c;return e("header",{staticClass:"navbar"},[e("SidebarButton",{on:{"toggle-sidebar":function(e){return t.$emit("toggle-sidebar")}}}),t._v(" "),e("RouterLink",{staticClass:"home-link",attrs:{to:t.$localePath}},[t.$site.themeConfig.logo?e("img",{staticClass:"logo",attrs:{src:t.$withBase(t.$site.themeConfig.logo),alt:t.$siteTitle}}):t._e(),t._v(" "),t.$siteTitle?e("span",{ref:"siteName",staticClass:"site-name",class:{"can-hide":t.$site.themeConfig.logo}},[t._v(t._s(t.$siteTitle))]):t._e()]),t._v(" "),e("div",{staticClass:"links",style:t.linksWrapMaxWidth?{"max-width":t.linksWrapMaxWidth+"px"}:{}},[t.isAlgoliaSearch?e("AlgoliaSearchBox",{attrs:{options:t.algolia}}):!1!==t.$site.themeConfig.search&&!1!==t.$page.frontmatter.search?e("SearchBox"):t._e(),t._v(" "),e("NavLinks",{staticClass:"can-hide"})],1)],1)}),[],!1,null,null,null));e.default=d.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[1,12,14,21,22,26],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return s})),n.d(e,"a",(function(){return r})),n.d(e,"i",(function(){return o})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return h})),n.d(e,"e",(function(){return f})),n.d(e,"k",(function(){return p})),n.d(e,"l",(function(){return d})),n.d(e,"c",(function(){return m})),n.d(e,"j",(function(){return k}));n(91);const s=/#.*$/,i=/\.(md|html)$/,r=/\/$/,o=/^[a-z]+:/i;function a(t){return decodeURI(t).replace(s,"").replace(i,"")}function l(t){return o.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function h(t){if(l(t))return t;const e=t.match(s),n=e?e[0]:"",i=a(t);return r.test(i)?t:i+".html"+n}function f(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(s);if(e)return e[0]}(e);if(i&&n!==i)return!1;return a(t.path)===a(e)}function p(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const s=t.charAt(0);if("/"===s)return t;if("?"===s||"#"===s)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const r=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,s,i=1){if("string"==typeof e)return p(n,e,s);if(Array.isArray(e))return Object.assign(p(n,e[0],s),{title:e[1]});{const r=e.children||[];return 0===r.length&&e.path?Object.assign(p(n,e.path,s),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:r.map(e=>t(e,n,s,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function g(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function k(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var s=n(242),i={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(s.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(s.g)(this.link)||Object(s.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(s.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(s.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},r=n(10),o=Object(r.a)(i,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=o.exports},245:function(t,e,n){"use strict";n.r(e);var s={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},i=(n(246),n(10)),r=Object(i.a)(s,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=r.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},250:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},257:function(t,e,n){"use strict";n.r(e);var s=n(244),i=n(245),r=n(92),o=n.n(r),a={name:"DropdownLink",components:{NavLink:s.default,DropdownTransition:i.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>o()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(a,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,s){return e("li",{key:n.link||s,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(s){return e("li",{key:s.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:s},on:{focusout:function(e){t.isLastItemOfArray(s,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},260:function(t,e,n){"use strict";n(250)},261:function(t,e,n){},270:function(t,e,n){"use strict";n.r(e);var s=n(257),i=n(242),r={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:s.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,s=this.$site.themeConfig.locales||{},i={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(i=>{const r=t[i],o=s[i]&&s[i].label||r.lang;let a;return r.lang===this.$lang?a=e:(a=e.replace(this.$localeConfig.path,i),n.some(t=>t.path===a)||(a=i)),{text:o,link:a}})};return[...this.userNav,i]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(i.j)(t),{items:(t.items||[]).map(i.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;n{let s=i()(e,"title","");return i()(e,"frontmatter.tags")&&(s+=" "+e.frontmatter.tags.join(" ")),n&&(s+=" "+n),o(t,s)};const o=(t,e)=>{const n=t=>t.replace(/[-/\\^$*+?.()|[\]{}]/g,"\\$&"),s=new RegExp("[^\0-]"),i=t.split(/\s+/g).map(t=>t.trim()).filter(t=>!!t);if(s.test(t))return i.some(t=>e.toLowerCase().indexOf(t)>-1);{const s=t.endsWith(" ");return new RegExp(i.map((t,e)=>i.length!==e+1||s?`(?=.*\\b${n(t)}\\b)`:`(?=.*\\b${n(t)})`).join("")+".+","gi").test(e)}};var a={name:"SearchBox",data:()=>({query:"",focused:!1,focusIndex:0,placeholder:void 0}),computed:{showSuggestions(){return this.focused&&this.suggestions&&this.suggestions.length},suggestions(){const t=this.query.trim().toLowerCase();if(!t)return;const{pages:e}=this.$site,n=this.$site.themeConfig.searchMaxSuggestions||5,s=this.$localePath,i=[];for(let o=0;o=n);o++){const a=e[o];if(this.getPageLocalePath(a)===s&&this.isSearchable(a))if(r(t,a))i.push(a);else if(a.headers)for(let e=0;e=n);e++){const n=a.headers[e];n.title&&r(t,a,n.title)&&i.push(Object.assign({},a,{path:a.path+"#"+n.slug,header:n}))}}return i},alignRight(){return(this.$site.themeConfig.nav||[]).length+(this.$site.repo?1:0)<=2}},mounted(){this.placeholder=this.$site.themeConfig.searchPlaceholder||"",document.addEventListener("keydown",this.onHotkey)},beforeDestroy(){document.removeEventListener("keydown",this.onHotkey)},methods:{getPageLocalePath(t){for(const e in this.$site.locales||{})if("/"!==e&&0===t.path.indexOf(e))return e;return"/"},isSearchable(t){let e=null;return null===e||(e=Array.isArray(e)?e:new Array(e),e.filter(e=>t.path.match(e)).length>0)},onHotkey(t){t.srcElement===document.body&&["s","/"].includes(t.key)&&(this.$refs.input.focus(),t.preventDefault())},onUp(){this.showSuggestions&&(this.focusIndex>0?this.focusIndex--:this.focusIndex=this.suggestions.length-1)},onDown(){this.showSuggestions&&(this.focusIndex "+t._s(n.header.title))]):t._e()])])})),0):t._e()])}),[],!1,null,null,null).exports,c=n(291),h=n(270);function f(t,e){return t.ownerDocument.defaultView.getComputedStyle(t,null)[e]}var p={name:"Navbar",components:{SidebarButton:c.default,NavLinks:h.default,SearchBox:u,AlgoliaSearchBox:{}},data:()=>({linksWrapMaxWidth:null}),computed:{algolia(){return this.$themeLocaleConfig.algolia||this.$site.themeConfig.algolia||{}},isAlgoliaSearch(){return this.algolia&&this.algolia.apiKey&&this.algolia.indexName}},mounted(){const t=parseInt(f(this.$el,"paddingLeft"))+parseInt(f(this.$el,"paddingRight")),e=()=>{document.documentElement.clientWidth<719?this.linksWrapMaxWidth=null:this.linksWrapMaxWidth=this.$el.offsetWidth-t-(this.$refs.siteName&&this.$refs.siteName.offsetWidth||0)};e(),window.addEventListener("resize",e,!1)}},d=(n(298),Object(l.a)(p,(function(){var t=this,e=t._self._c;return e("header",{staticClass:"navbar"},[e("SidebarButton",{on:{"toggle-sidebar":function(e){return t.$emit("toggle-sidebar")}}}),t._v(" "),e("RouterLink",{staticClass:"home-link",attrs:{to:t.$localePath}},[t.$site.themeConfig.logo?e("img",{staticClass:"logo",attrs:{src:t.$withBase(t.$site.themeConfig.logo),alt:t.$siteTitle}}):t._e(),t._v(" "),t.$siteTitle?e("span",{ref:"siteName",staticClass:"site-name",class:{"can-hide":t.$site.themeConfig.logo}},[t._v(t._s(t.$siteTitle))]):t._e()]),t._v(" "),e("div",{staticClass:"links",style:t.linksWrapMaxWidth?{"max-width":t.linksWrapMaxWidth+"px"}:{}},[t.isAlgoliaSearch?e("AlgoliaSearchBox",{attrs:{options:t.algolia}}):!1!==t.$site.themeConfig.search&&!1!==t.$page.frontmatter.search?e("SearchBox"):t._e(),t._v(" "),e("NavLinks",{staticClass:"can-hide"})],1)],1)}),[],!1,null,null,null));e.default=d.exports}}]); \ No newline at end of file diff --git a/assets/js/10.0ee1033c.js b/assets/js/10.b1af2f4e.js similarity index 93% rename from assets/js/10.0ee1033c.js rename to assets/js/10.b1af2f4e.js index fa8a00a..3df4a60 100644 --- a/assets/js/10.0ee1033c.js +++ b/assets/js/10.b1af2f4e.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[10,3,12,14,18,21,27],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return i})),n.d(e,"a",(function(){return r})),n.d(e,"i",(function(){return a})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return h})),n.d(e,"k",(function(){return d})),n.d(e,"l",(function(){return f})),n.d(e,"c",(function(){return m})),n.d(e,"j",(function(){return g}));n(91);const i=/#.*$/,s=/\.(md|html)$/,r=/\/$/,a=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(i,"").replace(s,"")}function l(t){return a.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function p(t){if(l(t))return t;const e=t.match(i),n=e?e[0]:"",s=o(t);return r.test(s)?t:s+".html"+n}function h(t,e){const n=decodeURIComponent(t.hash),s=function(t){const e=t.match(i);if(e)return e[0]}(e);if(s&&n!==s)return!1;return o(t.path)===o(e)}function d(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const i=t.charAt(0);if("/"===i)return t;if("?"===i||"#"===i)return e+t;const s=e.split("/");n&&s[s.length-1]||s.pop();const r=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,i,s=1){if("string"==typeof e)return d(n,e,i);if(Array.isArray(e))return Object.assign(d(n,e[0],i),{title:e[1]});{const r=e.children||[];return 0===r.length&&e.path?Object.assign(d(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:r.map(e=>t(e,n,i,s+1)),collapsable:!1!==e.collapsable}}}(t,s,n)):[]}return[]}function b(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),s={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},r=n(10),a=Object(r.a)(s,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=a.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},s=(n(246),n(10)),r=Object(s.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=r.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},248:function(t,e,n){},250:function(t,e,n){},253:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},255:function(t,e,n){"use strict";n(248)},258:function(t,e,n){"use strict";n.r(e);var i=n(271),s=n(260),r=n(242);function a(t,e){if("group"===e.type){const n=e.path&&Object(r.e)(t,e.path),i=e.children.some(e=>"group"===e.type?a(t,e):"page"===e.type&&Object(r.e)(t,e.path));return n||i}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:i.default,SidebarLink:s.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let n=0;n-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(r.e)(this.$route,t.regularPath)}}},l=n(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(n,i){return e("li",{key:i},["group"===n.type?e("SidebarGroup",{attrs:{item:n,open:i===t.openGroupIndex,collapsable:n.collapsable||n.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(i)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:n}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},259:function(t,e,n){"use strict";n.r(e);var i=n(244),s=n(245),r=n(92),a=n.n(r),o={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:s.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>a()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},260:function(t,e,n){"use strict";n.r(e);var i=n(242);function s(t,e,n,i,s){const r={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:i,"sidebar-link":!0}};return s>2&&(r.style={"padding-left":s+"rem"}),t("RouterLink",r,n)}function r(t,e,n,a,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(i.e)(a,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[s(t,n+"#"+e.slug,e.title,u,e.level-1),r(t,e.children,n,a,o,l+1)])}))}var a={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:a,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(i.e)(a,u.path),h="auto"===u.type?p||u.children.some(t=>Object(i.e)(a,u.basePath+"#"+t.slug)):p,d="external"===u.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,u.path,u.title||u.path):s(t,u.path,u.title||u.path,h),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[d,r(t,u.children,u.basePath,a,f)];if((h||b)&&u.headers&&!i.d.test(u.path)){return[d,r(t,Object(i.c)(u.headers),u.path,a,f)]}return d}},o=(n(255),n(10)),l=Object(o.a)(a,void 0,void 0,!1,null,null,null);e.default=l.exports},262:function(t,e,n){"use strict";n(250)},268:function(t,e,n){"use strict";n(253)},269:function(t,e,n){},270:function(t,e,n){"use strict";n.r(e);var i=n(259),s=n(242),r={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:i.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,i=this.$site.themeConfig.locales||{},s={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(s=>{const r=t[s],a=i[s]&&i[s].label||r.lang;let o;return r.lang===this.$lang?o=e:(o=e.replace(this.$localeConfig.path,s),n.some(t=>t.path===o)||(o=s)),{text:a,link:o}})};return[...this.userNav,s]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(s.j)(t),{items:(t.items||[]).map(s.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;nfunction t(e,n,i,s=1){if("string"==typeof e)return d(n,e,i);if(Array.isArray(e))return Object.assign(d(n,e[0],i),{title:e[1]});{const r=e.children||[];return 0===r.length&&e.path?Object.assign(d(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:r.map(e=>t(e,n,i,s+1)),collapsable:!1!==e.collapsable}}}(t,s,n)):[]}return[]}function b(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),s={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},r=n(10),a=Object(r.a)(s,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=a.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},s=(n(246),n(10)),r=Object(s.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=r.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},248:function(t,e,n){},250:function(t,e,n){},253:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},255:function(t,e,n){"use strict";n(248)},256:function(t,e,n){"use strict";n.r(e);var i=n(271),s=n(258),r=n(242);function a(t,e){if("group"===e.type){const n=e.path&&Object(r.e)(t,e.path),i=e.children.some(e=>"group"===e.type?a(t,e):"page"===e.type&&Object(r.e)(t,e.path));return n||i}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:i.default,SidebarLink:s.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let n=0;n-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(r.e)(this.$route,t.regularPath)}}},l=n(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(n,i){return e("li",{key:i},["group"===n.type?e("SidebarGroup",{attrs:{item:n,open:i===t.openGroupIndex,collapsable:n.collapsable||n.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(i)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:n}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},257:function(t,e,n){"use strict";n.r(e);var i=n(244),s=n(245),r=n(92),a=n.n(r),o={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:s.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>a()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},258:function(t,e,n){"use strict";n.r(e);var i=n(242);function s(t,e,n,i,s){const r={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:i,"sidebar-link":!0}};return s>2&&(r.style={"padding-left":s+"rem"}),t("RouterLink",r,n)}function r(t,e,n,a,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(i.e)(a,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[s(t,n+"#"+e.slug,e.title,u,e.level-1),r(t,e.children,n,a,o,l+1)])}))}var a={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:a,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(i.e)(a,u.path),h="auto"===u.type?p||u.children.some(t=>Object(i.e)(a,u.basePath+"#"+t.slug)):p,d="external"===u.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,u.path,u.title||u.path):s(t,u.path,u.title||u.path,h),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[d,r(t,u.children,u.basePath,a,f)];if((h||b)&&u.headers&&!i.d.test(u.path)){return[d,r(t,Object(i.c)(u.headers),u.path,a,f)]}return d}},o=(n(255),n(10)),l=Object(o.a)(a,void 0,void 0,!1,null,null,null);e.default=l.exports},260:function(t,e,n){"use strict";n(250)},266:function(t,e,n){"use strict";n(253)},267:function(t,e,n){},270:function(t,e,n){"use strict";n.r(e);var i=n(257),s=n(242),r={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:i.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,i=this.$site.themeConfig.locales||{},s={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(s=>{const r=t[s],a=i[s]&&i[s].label||r.lang;let o;return r.lang===this.$lang?o=e:(o=e.replace(this.$localeConfig.path,s),n.some(t=>t.path===o)||(o=s)),{text:a,link:o}})};return[...this.userNav,s]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(s.j)(t),{items:(t.items||[]).map(s.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;nfunction t(e,n,r,i=1){if("string"==typeof e)return d(n,e,r);if(Array.isArray(e))return Object.assign(d(n,e[0],r),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function g(t){const e=v(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function v(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,n){},252:function(t,e,n){},264:function(t,e,n){"use strict";n(251)},265:function(t,e,n){var r=n(12),i=n(4),a=n(11);t.exports=function(t){return"string"==typeof t||!i(t)&&a(t)&&"[object String]"==r(t)}},266:function(t,e,n){"use strict";n(252)},267:function(t,e,n){},272:function(t,e,n){"use strict";n.r(e);var r=n(249),i=n.n(r),a=n(242),s={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=i()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:n="",docsBranch:r="master",docsRepo:a=e}=this.$site.themeConfig;return t&&a&&this.$page.relativePath?this.createEditLink(e,a,n,r,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,n,r,i){if(/bitbucket.org/.test(e)){return e.replace(a.a,"")+"/src"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i+`?mode=edit&spa=0&at=${r}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(a.a,"")+"/-/edit"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i}return(a.i.test(e)?e:"https://github.com/"+e).replace(a.a,"")+"/edit"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i}}},o=(n(264),n(10)),u=Object(o.a)(s,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=u.exports},273:function(t,e,n){"use strict";n.r(e);n(91);var r=n(242),i=n(265),a=n.n(i),s=n(249),o=n.n(s),u={name:"PageNav",props:["sidebarItems"],computed:{prev(){return l(c.PREV,this)},next(){return l(c.NEXT,this)}}};const c={NEXT:{resolveLink:function(t,e){return p(t,e,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,e){return p(t,e,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function l(t,{$themeConfig:e,$page:n,$route:i,$site:s,sidebarItems:u}){const{resolveLink:c,getThemeLinkConfig:l,getPageLinkConfig:p}=t,f=l(e),d=p(n),h=o()(d)?f:d;return!1===h?void 0:a()(h)?Object(r.k)(s.pages,h,i.path):c(n,u)}function p(t,e,n){const r=[];!function t(e,n){for(let r=0,i=e.length;rfunction t(e,n,r,i=1){if("string"==typeof e)return d(n,e,r);if(Array.isArray(e))return Object.assign(d(n,e[0],r),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function g(t){const e=v(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function v(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,n){},252:function(t,e,n){},262:function(t,e,n){"use strict";n(251)},263:function(t,e,n){var r=n(12),i=n(4),a=n(11);t.exports=function(t){return"string"==typeof t||!i(t)&&a(t)&&"[object String]"==r(t)}},264:function(t,e,n){"use strict";n(252)},265:function(t,e,n){},272:function(t,e,n){"use strict";n.r(e);var r=n(249),i=n.n(r),a=n(242),s={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=i()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:n="",docsBranch:r="master",docsRepo:a=e}=this.$site.themeConfig;return t&&a&&this.$page.relativePath?this.createEditLink(e,a,n,r,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,n,r,i){if(/bitbucket.org/.test(e)){return e.replace(a.a,"")+"/src"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i+`?mode=edit&spa=0&at=${r}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(a.a,"")+"/-/edit"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i}return(a.i.test(e)?e:"https://github.com/"+e).replace(a.a,"")+"/edit"+`/${r}/`+(n?n.replace(a.a,"")+"/":"")+i}}},o=(n(262),n(10)),u=Object(o.a)(s,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=u.exports},273:function(t,e,n){"use strict";n.r(e);n(91);var r=n(242),i=n(263),a=n.n(i),s=n(249),o=n.n(s),u={name:"PageNav",props:["sidebarItems"],computed:{prev(){return l(c.PREV,this)},next(){return l(c.NEXT,this)}}};const c={NEXT:{resolveLink:function(t,e){return p(t,e,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,e){return p(t,e,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function l(t,{$themeConfig:e,$page:n,$route:i,$site:s,sidebarItems:u}){const{resolveLink:c,getThemeLinkConfig:l,getPageLinkConfig:p}=t,f=l(e),d=p(n),h=o()(d)?f:d;return!1===h?void 0:a()(h)?Object(r.k)(s.pages,h,i.path):c(n,u)}function p(t,e,n){const r=[];!function t(e,n){for(let r=0,i=e.length;rfunction t(e,n,i,r=1){if("string"==typeof e)return f(n,e,i);if(Array.isArray(e))return Object.assign(f(n,e[0],i),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(f(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function b(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=n(10),a=Object(s.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=a.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},r=(n(246),n(10)),s=Object(r.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},250:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},259:function(t,e,n){"use strict";n.r(e);var i=n(244),r=n(245),s=n(92),a=n.n(s),o={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:r.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>a()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},262:function(t,e,n){"use strict";n(250)},270:function(t,e,n){"use strict";n.r(e);var i=n(259),r=n(242),s={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:i.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,i=this.$site.themeConfig.locales||{},r={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(r=>{const s=t[r],a=i[r]&&i[r].label||s.lang;let o;return s.lang===this.$lang?o=e:(o=e.replace(this.$localeConfig.path,r),n.some(t=>t.path===o)||(o=r)),{text:a,link:o}})};return[...this.userNav,r]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(r.j)(t),{items:(t.items||[]).map(r.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;nfunction t(e,n,i,r=1){if("string"==typeof e)return f(n,e,i);if(Array.isArray(e))return Object.assign(f(n,e[0],i),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(f(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function b(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=n(10),a=Object(s.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=a.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},r=(n(246),n(10)),s=Object(r.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},250:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},257:function(t,e,n){"use strict";n.r(e);var i=n(244),r=n(245),s=n(92),a=n.n(s),o={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:r.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>a()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports},260:function(t,e,n){"use strict";n(250)},270:function(t,e,n){"use strict";n.r(e);var i=n(257),r=n(242),s={name:"NavLinks",components:{NavLink:n(244).default,DropdownLink:i.default},computed:{userNav(){return this.$themeLocaleConfig.nav||this.$site.themeConfig.nav||[]},nav(){const{locales:t}=this.$site;if(t&&Object.keys(t).length>1){const e=this.$page.path,n=this.$router.options.routes,i=this.$site.themeConfig.locales||{},r={text:this.$themeLocaleConfig.selectText||"Languages",ariaLabel:this.$themeLocaleConfig.ariaLabel||"Select language",items:Object.keys(t).map(r=>{const s=t[r],a=i[r]&&i[r].label||s.lang;let o;return s.lang===this.$lang?o=e:(o=e.replace(this.$localeConfig.path,r),n.some(t=>t.path===o)||(o=r)),{text:a,link:o}})};return[...this.userNav,r]}return this.userNav},userLinks(){return(this.nav||[]).map(t=>Object.assign(Object(r.j)(t),{items:(t.items||[]).map(r.j)}))},repoLink(){const{repo:t}=this.$site.themeConfig;return t?/^https?:/.test(t)?t:"https://github.com/"+t:null},repoLabel(){if(!this.repoLink)return;if(this.$site.themeConfig.repoLabel)return this.$site.themeConfig.repoLabel;const t=this.repoLink.match(/^https?:\/\/[^/]+/)[0],e=["GitHub","GitLab","Bitbucket"];for(let n=0;nfunction t(e,n,i,r=1){if("string"==typeof e)return h(n,e,i);if(Array.isArray(e))return Object.assign(h(n,e[0],i),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(h(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function b(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=n(10),o=Object(s.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=o.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},r=(n(246),n(10)),s=Object(r.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},259:function(t,e,n){"use strict";n.r(e);var i=n(244),r=n(245),s=n(92),o=n.n(s),a={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:r.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>o()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(a,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[14,21,26],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return i})),n.d(e,"a",(function(){return s})),n.d(e,"i",(function(){return o})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return f})),n.d(e,"k",(function(){return h})),n.d(e,"l",(function(){return d})),n.d(e,"c",(function(){return b})),n.d(e,"j",(function(){return g}));n(91);const i=/#.*$/,r=/\.(md|html)$/,s=/\/$/,o=/^[a-z]+:/i;function a(t){return decodeURI(t).replace(i,"").replace(r,"")}function l(t){return o.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function p(t){if(l(t))return t;const e=t.match(i),n=e?e[0]:"",r=a(t);return s.test(r)?t:r+".html"+n}function f(t,e){const n=decodeURIComponent(t.hash),r=function(t){const e=t.match(i);if(e)return e[0]}(e);if(r&&n!==r)return!1;return a(t.path)===a(e)}function h(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const i=t.charAt(0);if("/"===i)return t;if("?"===i||"#"===i)return e+t;const r=e.split("/");n&&r[r.length-1]||r.pop();const s=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,i,r=1){if("string"==typeof e)return h(n,e,i);if(Array.isArray(e))return Object.assign(h(n,e[0],i),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(h(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function b(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function g(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=n(10),o=Object(s.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=o.exports},245:function(t,e,n){"use strict";n.r(e);var i={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},r=(n(246),n(10)),s=Object(r.a)(i,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},247:function(t,e,n){},254:function(t,e,n){"use strict";n(247)},257:function(t,e,n){"use strict";n.r(e);var i=n(244),r=n(245),s=n(92),o=n.n(s),a={name:"DropdownLink",components:{NavLink:i.default,DropdownTransition:r.default},props:{item:{required:!0}},data:()=>({open:!1}),computed:{dropdownAriaLabel(){return this.item.ariaLabel||this.item.text}},watch:{$route(){this.open=!1}},methods:{setOpen(t){this.open=t},isLastItemOfArray:(t,e)=>o()(e)===t,handleDropdown(){0===event.detail&&this.setOpen(!this.open)}}},l=(n(254),n(10)),u=Object(l.a)(a,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"dropdown-wrapper",class:{open:t.open}},[e("button",{staticClass:"dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:t.handleDropdown}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow down"})]),t._v(" "),e("button",{staticClass:"mobile-dropdown-title",attrs:{type:"button","aria-label":t.dropdownAriaLabel},on:{click:function(e){return t.setOpen(!t.open)}}},[e("span",{staticClass:"title"},[t._v(t._s(t.item.text))]),t._v(" "),e("span",{staticClass:"arrow",class:t.open?"down":"right"})]),t._v(" "),e("DropdownTransition",[e("ul",{directives:[{name:"show",rawName:"v-show",value:t.open,expression:"open"}],staticClass:"nav-dropdown"},t._l(t.item.items,(function(n,i){return e("li",{key:n.link||i,staticClass:"dropdown-item"},["links"===n.type?e("h4",[t._v("\n "+t._s(n.text)+"\n ")]):t._e(),t._v(" "),"links"===n.type?e("ul",{staticClass:"dropdown-subitem-wrapper"},t._l(n.items,(function(i){return e("li",{key:i.link,staticClass:"dropdown-subitem"},[e("NavLink",{attrs:{item:i},on:{focusout:function(e){t.isLastItemOfArray(i,n.items)&&t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0):e("NavLink",{attrs:{item:n},on:{focusout:function(e){t.isLastItemOfArray(n,t.item.items)&&t.setOpen(!1)}}})],1)})),0)])],1)}),[],!1,null,null,null);e.default=u.exports}}]); \ No newline at end of file diff --git a/assets/js/15.f5494b4f.js b/assets/js/15.e67d9214.js similarity index 94% rename from assets/js/15.f5494b4f.js rename to assets/js/15.e67d9214.js index 36843f5..5502fc4 100644 --- a/assets/js/15.f5494b4f.js +++ b/assets/js/15.e67d9214.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[15],{242:function(t,n,e){"use strict";e.d(n,"d",(function(){return r})),e.d(n,"a",(function(){return o})),e.d(n,"i",(function(){return a})),e.d(n,"f",(function(){return u})),e.d(n,"g",(function(){return c})),e.d(n,"h",(function(){return p})),e.d(n,"b",(function(){return l})),e.d(n,"e",(function(){return f})),e.d(n,"k",(function(){return h})),e.d(n,"l",(function(){return d})),e.d(n,"c",(function(){return v})),e.d(n,"j",(function(){return b}));e(91);const r=/#.*$/,i=/\.(md|html)$/,o=/\/$/,a=/^[a-z]+:/i;function s(t){return decodeURI(t).replace(r,"").replace(i,"")}function u(t){return a.test(t)}function c(t){return/^mailto:/.test(t)}function p(t){return/^tel:/.test(t)}function l(t){if(u(t))return t;const n=t.match(r),e=n?n[0]:"",i=s(t);return o.test(i)?t:i+".html"+e}function f(t,n){const e=decodeURIComponent(t.hash),i=function(t){const n=t.match(r);if(n)return n[0]}(n);if(i&&e!==i)return!1;return s(t.path)===s(n)}function h(t,n,e){if(u(n))return{type:"external",path:n};e&&(n=function(t,n,e){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return n+t;const i=n.split("/");e&&i[i.length-1]||i.pop();const o=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(n,e,r,i=1){if("string"==typeof n)return h(e,n,r);if(Array.isArray(n))return Object.assign(h(e,n[0],r),{title:n[1]});{const o=n.children||[];return 0===o.length&&n.path?Object.assign(h(e,n.path,r),{title:n.title}):{type:"group",path:n.path,title:n.title,sidebarDepth:n.sidebarDepth,initialOpenGroupIndex:n.initialOpenGroupIndex,children:o.map(n=>t(n,e,r,i+1)),collapsable:!1!==n.collapsable}}}(t,i,e)):[]}return[]}function g(t){const n=v(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:n.map(n=>({type:"auto",title:n.title,basePath:t.path,path:t.path+"#"+n.slug,children:n.children||[]}))}]}function v(t){let n;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?n=t:n&&(n.children||(n.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,n){t.exports=function(t){return null==t}},252:function(t,n,e){},265:function(t,n,e){var r=e(12),i=e(4),o=e(11);t.exports=function(t){return"string"==typeof t||!i(t)&&o(t)&&"[object String]"==r(t)}},266:function(t,n,e){"use strict";e(252)},273:function(t,n,e){"use strict";e.r(n);e(91);var r=e(242),i=e(265),o=e.n(i),a=e(249),s=e.n(a),u={name:"PageNav",props:["sidebarItems"],computed:{prev(){return p(c.PREV,this)},next(){return p(c.NEXT,this)}}};const c={NEXT:{resolveLink:function(t,n){return l(t,n,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,n){return l(t,n,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function p(t,{$themeConfig:n,$page:e,$route:i,$site:a,sidebarItems:u}){const{resolveLink:c,getThemeLinkConfig:p,getPageLinkConfig:l}=t,f=p(n),h=l(e),d=s()(h)?f:h;return!1===d?void 0:o()(d)?Object(r.k)(a.pages,d,i.path):c(e,u)}function l(t,n,e){const r=[];!function t(n,e){for(let r=0,i=n.length;rfunction t(n,e,r,i=1){if("string"==typeof n)return h(e,n,r);if(Array.isArray(n))return Object.assign(h(e,n[0],r),{title:n[1]});{const o=n.children||[];return 0===o.length&&n.path?Object.assign(h(e,n.path,r),{title:n.title}):{type:"group",path:n.path,title:n.title,sidebarDepth:n.sidebarDepth,initialOpenGroupIndex:n.initialOpenGroupIndex,children:o.map(n=>t(n,e,r,i+1)),collapsable:!1!==n.collapsable}}}(t,i,e)):[]}return[]}function g(t){const n=v(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:n.map(n=>({type:"auto",title:n.title,basePath:t.path,path:t.path+"#"+n.slug,children:n.children||[]}))}]}function v(t){let n;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?n=t:n&&(n.children||(n.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,n){t.exports=function(t){return null==t}},252:function(t,n,e){},263:function(t,n,e){var r=e(12),i=e(4),o=e(11);t.exports=function(t){return"string"==typeof t||!i(t)&&o(t)&&"[object String]"==r(t)}},264:function(t,n,e){"use strict";e(252)},273:function(t,n,e){"use strict";e.r(n);e(91);var r=e(242),i=e(263),o=e.n(i),a=e(249),s=e.n(a),u={name:"PageNav",props:["sidebarItems"],computed:{prev(){return p(c.PREV,this)},next(){return p(c.NEXT,this)}}};const c={NEXT:{resolveLink:function(t,n){return l(t,n,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,n){return l(t,n,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function p(t,{$themeConfig:n,$page:e,$route:i,$site:a,sidebarItems:u}){const{resolveLink:c,getThemeLinkConfig:p,getPageLinkConfig:l}=t,f=p(n),h=l(e),d=s()(h)?f:h;return!1===d?void 0:o()(d)?Object(r.k)(a.pages,d,i.path):c(e,u)}function l(t,n,e){const r=[];!function t(n,e){for(let r=0,i=n.length;rfunction t(e,n,i,r=1){if("string"==typeof e)return d(n,e,i);if(Array.isArray(e))return Object.assign(d(n,e[0],i),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},a=n(10),s=Object(a.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=s.exports},261:function(t,e,n){},274:function(t,e,n){"use strict";n(261)},288:function(t,e,n){"use strict";n.r(e);var i={name:"Home",components:{NavLink:n(244).default},computed:{data(){return this.$page.frontmatter},actionLink(){return{link:this.data.actionLink,text:this.data.actionText}}}},r=(n(274),n(10)),a=Object(r.a)(i,(function(){var t=this,e=t._self._c;return e("main",{staticClass:"home",attrs:{"aria-labelledby":null!==t.data.heroText?"main-title":null}},[e("header",{staticClass:"hero"},[t.data.heroImage?e("img",{attrs:{src:t.$withBase(t.data.heroImage),alt:t.data.heroAlt||"hero"}}):t._e(),t._v(" "),null!==t.data.heroText?e("h1",{attrs:{id:"main-title"}},[t._v("\n "+t._s(t.data.heroText||t.$title||"Hello")+"\n ")]):t._e(),t._v(" "),null!==t.data.tagline?e("p",{staticClass:"description"},[t._v("\n "+t._s(t.data.tagline||t.$description||"Welcome to your VuePress site")+"\n ")]):t._e(),t._v(" "),t.data.actionText&&t.data.actionLink?e("p",{staticClass:"action"},[e("NavLink",{staticClass:"action-button",attrs:{item:t.actionLink}})],1):t._e()]),t._v(" "),t.data.features&&t.data.features.length?e("div",{staticClass:"features"},t._l(t.data.features,(function(n,i){return e("div",{key:i,staticClass:"feature"},[e("h2",[t._v(t._s(n.title))]),t._v(" "),e("p",[t._v(t._s(n.details))])])})),0):t._e(),t._v(" "),e("Content",{staticClass:"theme-default-content custom"}),t._v(" "),t.data.footer?e("div",{staticClass:"footer"},[t._v("\n "+t._s(t.data.footer)+"\n ")]):e("Content",{staticClass:"footer",attrs:{"slot-key":"footer"}})],1)}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[16,26],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return i})),n.d(e,"a",(function(){return a})),n.d(e,"i",(function(){return s})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return f})),n.d(e,"e",(function(){return h})),n.d(e,"k",(function(){return d})),n.d(e,"l",(function(){return p})),n.d(e,"c",(function(){return g})),n.d(e,"j",(function(){return b}));n(91);const i=/#.*$/,r=/\.(md|html)$/,a=/\/$/,s=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(i,"").replace(r,"")}function l(t){return s.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function f(t){if(l(t))return t;const e=t.match(i),n=e?e[0]:"",r=o(t);return a.test(r)?t:r+".html"+n}function h(t,e){const n=decodeURIComponent(t.hash),r=function(t){const e=t.match(i);if(e)return e[0]}(e);if(r&&n!==r)return!1;return o(t.path)===o(e)}function d(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const i=t.charAt(0);if("/"===i)return t;if("?"===i||"#"===i)return e+t;const r=e.split("/");n&&r[r.length-1]||r.pop();const a=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,i,r=1){if("string"==typeof e)return d(n,e,i);if(Array.isArray(e))return Object.assign(d(n,e[0],i),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function m(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},244:function(t,e,n){"use strict";n.r(e);var i=n(242),r={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(i.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(i.g)(this.link)||Object(i.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(i.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(i.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},a=n(10),s=Object(a.a)(r,(function(){var t=this,e=t._self._c;return t.isInternal?e("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(e){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):e("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?e("OutboundLink"):t._e()],1)}),[],!1,null,null,null);e.default=s.exports},259:function(t,e,n){},274:function(t,e,n){"use strict";n(259)},288:function(t,e,n){"use strict";n.r(e);var i={name:"Home",components:{NavLink:n(244).default},computed:{data(){return this.$page.frontmatter},actionLink(){return{link:this.data.actionLink,text:this.data.actionText}}}},r=(n(274),n(10)),a=Object(r.a)(i,(function(){var t=this,e=t._self._c;return e("main",{staticClass:"home",attrs:{"aria-labelledby":null!==t.data.heroText?"main-title":null}},[e("header",{staticClass:"hero"},[t.data.heroImage?e("img",{attrs:{src:t.$withBase(t.data.heroImage),alt:t.data.heroAlt||"hero"}}):t._e(),t._v(" "),null!==t.data.heroText?e("h1",{attrs:{id:"main-title"}},[t._v("\n "+t._s(t.data.heroText||t.$title||"Hello")+"\n ")]):t._e(),t._v(" "),null!==t.data.tagline?e("p",{staticClass:"description"},[t._v("\n "+t._s(t.data.tagline||t.$description||"Welcome to your VuePress site")+"\n ")]):t._e(),t._v(" "),t.data.actionText&&t.data.actionLink?e("p",{staticClass:"action"},[e("NavLink",{staticClass:"action-button",attrs:{item:t.actionLink}})],1):t._e()]),t._v(" "),t.data.features&&t.data.features.length?e("div",{staticClass:"features"},t._l(t.data.features,(function(n,i){return e("div",{key:i,staticClass:"feature"},[e("h2",[t._v(t._s(n.title))]),t._v(" "),e("p",[t._v(t._s(n.details))])])})),0):t._e(),t._v(" "),e("Content",{staticClass:"theme-default-content custom"}),t._v(" "),t.data.footer?e("div",{staticClass:"footer"},[t._v("\n "+t._s(t.data.footer)+"\n ")]):e("Content",{staticClass:"footer",attrs:{"slot-key":"footer"}})],1)}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/17.b17c6981.js b/assets/js/17.c4879b3a.js similarity index 97% rename from assets/js/17.b17c6981.js rename to assets/js/17.c4879b3a.js index b50ab49..98dcc9e 100644 --- a/assets/js/17.b17c6981.js +++ b/assets/js/17.c4879b3a.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[17],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return i})),n.d(e,"a",(function(){return a})),n.d(e,"i",(function(){return s})),n.d(e,"f",(function(){return c})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return l})),n.d(e,"b",(function(){return d})),n.d(e,"e",(function(){return p})),n.d(e,"k",(function(){return f})),n.d(e,"l",(function(){return h})),n.d(e,"c",(function(){return m})),n.d(e,"j",(function(){return b}));n(91);const i=/#.*$/,r=/\.(md|html)$/,a=/\/$/,s=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(i,"").replace(r,"")}function c(t){return s.test(t)}function u(t){return/^mailto:/.test(t)}function l(t){return/^tel:/.test(t)}function d(t){if(c(t))return t;const e=t.match(i),n=e?e[0]:"",r=o(t);return a.test(r)?t:r+".html"+n}function p(t,e){const n=decodeURIComponent(t.hash),r=function(t){const e=t.match(i);if(e)return e[0]}(e);if(r&&n!==r)return!1;return o(t.path)===o(e)}function f(t,e,n){if(c(e))return{type:"external",path:e};n&&(e=function(t,e,n){const i=t.charAt(0);if("/"===i)return t;if("?"===i||"#"===i)return e+t;const r=e.split("/");n&&r[r.length-1]||r.pop();const a=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,i,r=1){if("string"==typeof e)return f(n,e,i);if(Array.isArray(e))return Object.assign(f(n,e[0],i),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(f(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function g(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,n){},264:function(t,e,n){"use strict";n(251)},272:function(t,e,n){"use strict";n.r(e);var i=n(249),r=n.n(i),a=n(242),s={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=r()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:n="",docsBranch:i="master",docsRepo:a=e}=this.$site.themeConfig;return t&&a&&this.$page.relativePath?this.createEditLink(e,a,n,i,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,n,i,r){if(/bitbucket.org/.test(e)){return e.replace(a.a,"")+"/src"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r+`?mode=edit&spa=0&at=${i}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(a.a,"")+"/-/edit"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r}return(a.i.test(e)?e:"https://github.com/"+e).replace(a.a,"")+"/edit"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r}}},o=(n(264),n(10)),c=Object(o.a)(s,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=c.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[17],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return i})),n.d(e,"a",(function(){return a})),n.d(e,"i",(function(){return s})),n.d(e,"f",(function(){return c})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return l})),n.d(e,"b",(function(){return d})),n.d(e,"e",(function(){return p})),n.d(e,"k",(function(){return f})),n.d(e,"l",(function(){return h})),n.d(e,"c",(function(){return m})),n.d(e,"j",(function(){return b}));n(91);const i=/#.*$/,r=/\.(md|html)$/,a=/\/$/,s=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(i,"").replace(r,"")}function c(t){return s.test(t)}function u(t){return/^mailto:/.test(t)}function l(t){return/^tel:/.test(t)}function d(t){if(c(t))return t;const e=t.match(i),n=e?e[0]:"",r=o(t);return a.test(r)?t:r+".html"+n}function p(t,e){const n=decodeURIComponent(t.hash),r=function(t){const e=t.match(i);if(e)return e[0]}(e);if(r&&n!==r)return!1;return o(t.path)===o(e)}function f(t,e,n){if(c(e))return{type:"external",path:e};n&&(e=function(t,e,n){const i=t.charAt(0);if("/"===i)return t;if("?"===i||"#"===i)return e+t;const r=e.split("/");n&&r[r.length-1]||r.pop();const a=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,i,r=1){if("string"==typeof e)return f(n,e,i);if(Array.isArray(e))return Object.assign(f(n,e[0],i),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(f(n,e.path,i),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,i,r+1)),collapsable:!1!==e.collapsable}}}(t,r,n)):[]}return[]}function g(t){const e=m(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function m(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function b(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,n){},262:function(t,e,n){"use strict";n(251)},272:function(t,e,n){"use strict";n.r(e);var i=n(249),r=n.n(i),a=n(242),s={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=r()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:n="",docsBranch:i="master",docsRepo:a=e}=this.$site.themeConfig;return t&&a&&this.$page.relativePath?this.createEditLink(e,a,n,i,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,n,i,r){if(/bitbucket.org/.test(e)){return e.replace(a.a,"")+"/src"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r+`?mode=edit&spa=0&at=${i}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(a.a,"")+"/-/edit"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r}return(a.i.test(e)?e:"https://github.com/"+e).replace(a.a,"")+"/edit"+`/${i}/`+(n?n.replace(a.a,"")+"/":"")+r}}},o=(n(262),n(10)),c=Object(o.a)(s,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=c.exports}}]); \ No newline at end of file diff --git a/assets/js/18.1208b728.js b/assets/js/18.ed87e536.js similarity index 98% rename from assets/js/18.1208b728.js rename to assets/js/18.ed87e536.js index 21dada5..3875832 100644 --- a/assets/js/18.1208b728.js +++ b/assets/js/18.ed87e536.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[18],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return r})),n.d(e,"a",(function(){return a})),n.d(e,"i",(function(){return s})),n.d(e,"f",(function(){return o})),n.d(e,"g",(function(){return c})),n.d(e,"h",(function(){return l})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return f})),n.d(e,"k",(function(){return d})),n.d(e,"l",(function(){return h})),n.d(e,"c",(function(){return g})),n.d(e,"j",(function(){return m}));n(91);const r=/#.*$/,i=/\.(md|html)$/,a=/\/$/,s=/^[a-z]+:/i;function u(t){return decodeURI(t).replace(r,"").replace(i,"")}function o(t){return s.test(t)}function c(t){return/^mailto:/.test(t)}function l(t){return/^tel:/.test(t)}function p(t){if(o(t))return t;const e=t.match(r),n=e?e[0]:"",i=u(t);return a.test(i)?t:i+".html"+n}function f(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(r);if(e)return e[0]}(e);if(i&&n!==i)return!1;return u(t.path)===u(e)}function d(t,e,n){if(o(e))return{type:"external",path:e};n&&(e=function(t,e,n){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const a=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,r,i=1){if("string"==typeof e)return d(n,e,r);if(Array.isArray(e))return Object.assign(d(n,e[0],r),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function b(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},248:function(t,e,n){},255:function(t,e,n){"use strict";n(248)},260:function(t,e,n){"use strict";n.r(e);var r=n(242);function i(t,e,n,r,i){const a={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:r,"sidebar-link":!0}};return i>2&&(a.style={"padding-left":i+"rem"}),t("RouterLink",a,n)}function a(t,e,n,s,u,o=1){return!e||o>u?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const c=Object(r.e)(s,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[i(t,n+"#"+e.slug,e.title,c,e.level-1),a(t,e.children,n,s,u,o+1)])}))}var s={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:s,$themeConfig:u,$themeLocaleConfig:o},props:{item:c,sidebarDepth:l}}){const p=Object(r.e)(s,c.path),f="auto"===c.type?p||c.children.some(t=>Object(r.e)(s,c.basePath+"#"+t.slug)):p,d="external"===c.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,c.path,c.title||c.path):i(t,c.path,c.title||c.path,f),h=[e.frontmatter.sidebarDepth,l,o.sidebarDepth,u.sidebarDepth,1].find(t=>void 0!==t),b=o.displayAllHeaders||u.displayAllHeaders;if("auto"===c.type)return[d,a(t,c.children,c.basePath,s,h)];if((f||b)&&c.headers&&!r.d.test(c.path)){return[d,a(t,Object(r.c)(c.headers),c.path,s,h)]}return d}},u=(n(255),n(10)),o=Object(u.a)(s,void 0,void 0,!1,null,null,null);e.default=o.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[18],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return r})),n.d(e,"a",(function(){return a})),n.d(e,"i",(function(){return s})),n.d(e,"f",(function(){return o})),n.d(e,"g",(function(){return c})),n.d(e,"h",(function(){return l})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return f})),n.d(e,"k",(function(){return d})),n.d(e,"l",(function(){return h})),n.d(e,"c",(function(){return g})),n.d(e,"j",(function(){return m}));n(91);const r=/#.*$/,i=/\.(md|html)$/,a=/\/$/,s=/^[a-z]+:/i;function u(t){return decodeURI(t).replace(r,"").replace(i,"")}function o(t){return s.test(t)}function c(t){return/^mailto:/.test(t)}function l(t){return/^tel:/.test(t)}function p(t){if(o(t))return t;const e=t.match(r),n=e?e[0]:"",i=u(t);return a.test(i)?t:i+".html"+n}function f(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(r);if(e)return e[0]}(e);if(i&&n!==i)return!1;return u(t.path)===u(e)}function d(t,e,n){if(o(e))return{type:"external",path:e};n&&(e=function(t,e,n){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const a=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,r,i=1){if("string"==typeof e)return d(n,e,r);if(Array.isArray(e))return Object.assign(d(n,e[0],r),{title:e[1]});{const a=e.children||[];return 0===a.length&&e.path?Object.assign(d(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:a.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function b(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},248:function(t,e,n){},255:function(t,e,n){"use strict";n(248)},258:function(t,e,n){"use strict";n.r(e);var r=n(242);function i(t,e,n,r,i){const a={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:r,"sidebar-link":!0}};return i>2&&(a.style={"padding-left":i+"rem"}),t("RouterLink",a,n)}function a(t,e,n,s,u,o=1){return!e||o>u?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const c=Object(r.e)(s,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[i(t,n+"#"+e.slug,e.title,c,e.level-1),a(t,e.children,n,s,u,o+1)])}))}var s={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:s,$themeConfig:u,$themeLocaleConfig:o},props:{item:c,sidebarDepth:l}}){const p=Object(r.e)(s,c.path),f="auto"===c.type?p||c.children.some(t=>Object(r.e)(s,c.basePath+"#"+t.slug)):p,d="external"===c.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,c.path,c.title||c.path):i(t,c.path,c.title||c.path,f),h=[e.frontmatter.sidebarDepth,l,o.sidebarDepth,u.sidebarDepth,1].find(t=>void 0!==t),b=o.displayAllHeaders||u.displayAllHeaders;if("auto"===c.type)return[d,a(t,c.children,c.basePath,s,h)];if((f||b)&&c.headers&&!r.d.test(c.path)){return[d,a(t,Object(r.c)(c.headers),c.path,s,h)]}return d}},u=(n(255),n(10)),o=Object(u.a)(s,void 0,void 0,!1,null,null,null);e.default=o.exports}}]); \ No newline at end of file diff --git a/assets/js/19.06ac8f8c.js b/assets/js/19.13959b08.js similarity index 99% rename from assets/js/19.06ac8f8c.js rename to assets/js/19.13959b08.js index 18deb4c..d6ba60e 100644 --- a/assets/js/19.06ac8f8c.js +++ b/assets/js/19.13959b08.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[19],{256:function(t,n){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},257:function(t,n,s){t.exports=s.p+"assets/img/image7.ca08b1e7.png"},278:function(t,n,s){t.exports=s.p+"assets/img/image8.5d2d558a.png"},319:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"injection-methods-supported-by-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#injection-methods-supported-by-gone"}},[t._v("#")]),t._v(" Injection Methods Supported by Gone")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#public-property-injection-vs-private-property-injection"}},[t._v("Public Property Injection vs Private Property Injection")])]),n("li",[n("a",{attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("Anonymous Injection vs Named Injection")]),n("ul",[n("li",[n("a",{attrs:{href:"#anonymous-burial-and-named-burial"}},[t._v("Anonymous Burial and Named Burial")])]),n("li",[n("a",{attrs:{href:"#anonymous-injection"}},[t._v("Anonymous Injection")])]),n("li",[n("a",{attrs:{href:"#named-injection"}},[t._v("Named Injection")])])])]),n("li",[n("a",{attrs:{href:"#supported-injection-property-types"}},[t._v("Supported Injection Property Types")]),n("ul",[n("li",[n("a",{attrs:{href:"#_1-pointer-types"}},[t._v("1. Pointer Types")])]),n("li",[n("a",{attrs:{href:"#_2-value-types"}},[t._v("2. Value Types")])]),n("li",[n("a",{attrs:{href:"#_3-interface-types"}},[t._v("3. Interface Types")])]),n("li",[n("a",{attrs:{href:"#_4-slice-types"}},[t._v("4. Slice Types")])]),n("li",[n("a",{attrs:{href:"#_5-map-types"}},[t._v("5. Map Types")])])])]),n("li",[n("a",{attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("Pointer Injection vs Interface Injection")])]),n("li",[n("a",{attrs:{href:"#slice-injection-vs-map-injection"}},[t._v("Slice Injection vs Map Injection")])])])]),n("p"),t._v(" "),n("h2",{attrs:{id:"public-property-injection-vs-private-property-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#public-property-injection-vs-private-property-injection"}},[t._v("#")]),t._v(" Public Property Injection vs Private Property Injection")]),t._v(" "),n("p",[t._v('In Gone, both public property injection and private property injection are supported. Dependency injection typically restricts the "objects" to be injected to be used only within the module. Following the "open-closed" principle, I recommend using private property injection.')]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("Recommendation: Private Property Injection")])]),t._v(" "),n("p",[t._v("For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// lowercase first letter indicates private property injection, accessible only within the package")]),t._v("\n Manager "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// uppercase first letter indicates public property injection, accessible without restrictions")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"anonymous-injection-vs-named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("#")]),t._v(" Anonymous Injection vs Named Injection")]),t._v(" "),n("h3",{attrs:{id:"anonymous-burial-and-named-burial"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-burial-and-named-burial"}},[t._v("#")]),t._v(" Anonymous Burial and Named Burial")]),t._v(" "),n("p",[t._v("To inject a Goner, it must first be loaded into the system. In Gone, this process is termed as "),n("strong",[t._v("burial")]),t._v(", done in the "),n("strong",[t._v("Priest")]),t._v(" function by invoking the "),n("code",[t._v("cemetery.Bury")]),t._v(' function to "bury" the reference of the Goner into the '),n("strong",[t._v("cemetery")]),t._v(". When burying, if no GonerId is specified, it's an "),n("strong",[t._v("anonymous burial")]),t._v("; if specified, it's a "),n("strong",[t._v("named burial")]),t._v(".")]),t._v(" "),n("p",[t._v("The complete definition of the "),n("strong",[n("code",[t._v("Cemetery.Bury")])]),t._v(" function is as follows: "),n("strong",[n("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(". This definition is designed with two considerations:")]),t._v(" "),n("ol",[n("li",[t._v("It supports "),n("strong",[t._v("named burials")]),t._v(", where the second parameter is optional, allowing passing a string as the "),n("strong",[t._v("Goner")]),t._v("'s "),n("strong",[t._v("ID")]),t._v(" ("),n("strong",[t._v("GonerId")]),t._v(").")]),t._v(" "),n("li",[t._v("It enables method chaining for the Bury function.")])]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named burial for the first AGoner, ID=A1")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named burial for the second AGoner, ID=A2")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous burial")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("In anonymous burial, if there exist multiple compatible types of "),n("strong",[t._v("Goner")]),t._v(", only one of them will be injected, typically the first one revived. This may lead to undefined behavior. For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n\tseller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection, if multiple Workers exist, only one will be injected, typically the first one")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"John"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Alice"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("h3",{attrs:{id:"anonymous-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection"}},[t._v("#")]),t._v(" Anonymous Injection")]),t._v(" "),n("p",[t._v("When injecting without specifying the Goner's GonerId, it's termed as anonymous injection, indicated by the injection tag "),n("code",[t._v('gone:"*"')]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("In anonymous injection, if there are multiple compatible types of "),n("strong",[t._v("Goner")]),t._v(", only one will be injected, typically the first one revived. This may lead to undefined behavior. For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n\tseller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection, if multiple Workers exist, only one will be injected, typically the first one")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"John"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Alice"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("h3",{attrs:{id:"named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#named-injection"}},[t._v("#")]),t._v(" Named Injection")]),t._v(" "),n("p",[t._v("When injecting with a specified GonerId, it's termed as named injection. Named injection accurately injects a Goner based on its name, which is useful in practical development. It's essential to ensure that a Goner with the corresponding GonerId is buried in the cemetery.")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n Manager "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"supported-injection-property-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#supported-injection-property-types"}},[t._v("#")]),t._v(" Supported Injection Property Types")]),t._v(" "),n("h3",{attrs:{id:"_1-pointer-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-pointer-types"}},[t._v("#")]),t._v(" 1. Pointer Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a pointer, it's termed as "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_2-value-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-value-types"}},[t._v("#")]),t._v(" 2. Value Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a value type, it's termed as "),n("strong",[t._v("value injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v('In Go, value types are passed by copy, meaning that when using value injection, a new "object" is created, and the old and new objects are independent in memory. This may lead to unexpected results. For example:')]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value injection")]),t._v("\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the above code, the printed result will be "),n("code",[t._v("false")]),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"_3-interface-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-interface-types"}},[t._v("#")]),t._v(" 3. Interface Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is an interface type, it's termed as "),n("strong",[t._v("interface injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// interface type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_4-slice-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-slice-types"}},[t._v("#")]),t._v(" 4. Slice Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a slice type, it's termed as "),n("strong",[t._v("slice injection")]),t._v('. In slice injection, the element types of the slice can be value types, pointer types, or interface types. Due to the "copy by pass" nature of value types, it\'s not recommended to use them.')]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as interface types")]),t._v("\n workers "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as pointer types")]),t._v("\n persons "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as value types (not recommended)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_5-map-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-map-types"}},[t._v("#")]),t._v(" 5. Map Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a map type, it's termed as "),n("strong",[t._v("map injection")]),t._v('. In map injection, the element types of the map can be value types, pointer types, or interface types. Due to the "copy by pass" nature of value types, it\'s not recommended to use them. The keys of the map are always of type '),n("code",[t._v("string")]),t._v(", and the value of each key is the GonerId of the injected Goner.")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as interface types")]),t._v("\n workers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as pointer types")]),t._v("\n persons "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as value types (not recommended)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"pointer-injection-vs-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection vs Interface Injection")]),t._v(" "),n("p",[t._v("During the Bury process of a Goner in Gone, it's required to pass a reference. Both pointer injection and interface injection can pass the reference obtained from the "),n("code",[t._v("Cemetery.Bury")]),t._v(" method to the properties of a structure. "),n("strong",[t._v("Pointer injection")]),t._v(" is simple and straightforward, with a one-to-one correspondence between types, requiring no further explanation. "),n("strong",[t._v("Interfaces")]),t._v(" in Go are designed to decouple business usage from business logic implementation, allowing the usage side to be unaware of the implementation details. Interfaces also help break circular dependencies, which are not allowed in Go due to its strong typing and compile-time checks. Here's how interfaces can be used to break circular dependencies between packages:")]),t._v(" "),n("p",[t._v("Circular dependency:\n"),n("img",{attrs:{src:s(256),alt:"Circular Dependency"}})]),t._v(" "),n("p",[t._v("Using interfaces to break the circular dependency:\n"),n("img",{attrs:{src:s(257),alt:"Using Interfaces to Break Circular Dependency"}})]),t._v(" "),n("p",[t._v('Using interfaces helps hide implementation details, reduces coupling between modules, and adheres better to the "open-closed" principle. Therefore, '),n("strong",[t._v("interface injection")]),t._v(" is recommended. However, there are no absolute rules, and introducing interfaces will increase additional costs. Hence, we still support "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"slice-injection-vs-map-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#slice-injection-vs-map-injection"}},[t._v("#")]),t._v(" Slice Injection vs Map Injection")]),t._v(" "),n("p",[t._v("Gone supports injecting into "),n("code",[t._v("Slice")]),t._v(" and "),n("code",[t._v("Map")]),t._v(", termed as "),n("strong",[t._v("slice injection")]),t._v(" and "),n("strong",[t._v("map injection")]),t._v(" respectively. In both cases, the elements of the Slice and Map can be value types, pointer types, or interface types. However, due to the \"copy by pass\" nature of value types, it's not recommended to use them. Here's how you can inject into Slice and Map properties:")]),t._v(" "),n("p",[n("strong",[t._v("Injection rules are as follows:")])]),t._v(" "),n("ul",[n("li",[t._v("Slice and Map can contain elements of Goner pointer types, Goner value types, or interfaces.")]),t._v(" "),n("li",[t._v("Gone injects all compatible types of Goners into Slice and Map.")]),t._v(" "),n("li",[t._v("Map keys are of type "),n("code",[t._v("string")]),t._v(", and the value of each key is the GonerId of the injected Goner. If no GonerId is specified during burial, Gone generates one.")])]),t._v(" "),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("Using value types as Slice and Map types is not recommended.")])]),t._v(" "),n("p",[t._v("Here's a complete example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as Goner pointer types")]),t._v("\n\taSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as Goner value types")]),t._v("\n\n\taMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as Goner pointer types")]),t._v("\n\taMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as Goner value types")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 constructs A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 constructs A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The example produces the following output:\n"),n("img",{attrs:{src:s(278),alt:"slice map injected example result"}})])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[19],{268:function(t,n){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},269:function(t,n,s){t.exports=s.p+"assets/img/image7.ca08b1e7.png"},278:function(t,n,s){t.exports=s.p+"assets/img/image8.5d2d558a.png"},321:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"injection-methods-supported-by-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#injection-methods-supported-by-gone"}},[t._v("#")]),t._v(" Injection Methods Supported by Gone")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#public-property-injection-vs-private-property-injection"}},[t._v("Public Property Injection vs Private Property Injection")])]),n("li",[n("a",{attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("Anonymous Injection vs Named Injection")]),n("ul",[n("li",[n("a",{attrs:{href:"#anonymous-burial-and-named-burial"}},[t._v("Anonymous Burial and Named Burial")])]),n("li",[n("a",{attrs:{href:"#anonymous-injection"}},[t._v("Anonymous Injection")])]),n("li",[n("a",{attrs:{href:"#named-injection"}},[t._v("Named Injection")])])])]),n("li",[n("a",{attrs:{href:"#supported-injection-property-types"}},[t._v("Supported Injection Property Types")]),n("ul",[n("li",[n("a",{attrs:{href:"#_1-pointer-types"}},[t._v("1. Pointer Types")])]),n("li",[n("a",{attrs:{href:"#_2-value-types"}},[t._v("2. Value Types")])]),n("li",[n("a",{attrs:{href:"#_3-interface-types"}},[t._v("3. Interface Types")])]),n("li",[n("a",{attrs:{href:"#_4-slice-types"}},[t._v("4. Slice Types")])]),n("li",[n("a",{attrs:{href:"#_5-map-types"}},[t._v("5. Map Types")])])])]),n("li",[n("a",{attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("Pointer Injection vs Interface Injection")])]),n("li",[n("a",{attrs:{href:"#slice-injection-vs-map-injection"}},[t._v("Slice Injection vs Map Injection")])])])]),n("p"),t._v(" "),n("h2",{attrs:{id:"public-property-injection-vs-private-property-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#public-property-injection-vs-private-property-injection"}},[t._v("#")]),t._v(" Public Property Injection vs Private Property Injection")]),t._v(" "),n("p",[t._v('In Gone, both public property injection and private property injection are supported. Dependency injection typically restricts the "objects" to be injected to be used only within the module. Following the "open-closed" principle, I recommend using private property injection.')]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("Recommendation: Private Property Injection")])]),t._v(" "),n("p",[t._v("For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// lowercase first letter indicates private property injection, accessible only within the package")]),t._v("\n Manager "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// uppercase first letter indicates public property injection, accessible without restrictions")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"anonymous-injection-vs-named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("#")]),t._v(" Anonymous Injection vs Named Injection")]),t._v(" "),n("h3",{attrs:{id:"anonymous-burial-and-named-burial"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-burial-and-named-burial"}},[t._v("#")]),t._v(" Anonymous Burial and Named Burial")]),t._v(" "),n("p",[t._v("To inject a Goner, it must first be loaded into the system. In Gone, this process is termed as "),n("strong",[t._v("burial")]),t._v(", done in the "),n("strong",[t._v("Priest")]),t._v(" function by invoking the "),n("code",[t._v("cemetery.Bury")]),t._v(' function to "bury" the reference of the Goner into the '),n("strong",[t._v("cemetery")]),t._v(". When burying, if no GonerId is specified, it's an "),n("strong",[t._v("anonymous burial")]),t._v("; if specified, it's a "),n("strong",[t._v("named burial")]),t._v(".")]),t._v(" "),n("p",[t._v("The complete definition of the "),n("strong",[n("code",[t._v("Cemetery.Bury")])]),t._v(" function is as follows: "),n("strong",[n("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(". This definition is designed with two considerations:")]),t._v(" "),n("ol",[n("li",[t._v("It supports "),n("strong",[t._v("named burials")]),t._v(", where the second parameter is optional, allowing passing a string as the "),n("strong",[t._v("Goner")]),t._v("'s "),n("strong",[t._v("ID")]),t._v(" ("),n("strong",[t._v("GonerId")]),t._v(").")]),t._v(" "),n("li",[t._v("It enables method chaining for the Bury function.")])]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named burial for the first AGoner, ID=A1")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named burial for the second AGoner, ID=A2")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous burial")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("In anonymous burial, if there exist multiple compatible types of "),n("strong",[t._v("Goner")]),t._v(", only one of them will be injected, typically the first one revived. This may lead to undefined behavior. For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n\tseller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection, if multiple Workers exist, only one will be injected, typically the first one")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"John"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Alice"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("h3",{attrs:{id:"anonymous-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection"}},[t._v("#")]),t._v(" Anonymous Injection")]),t._v(" "),n("p",[t._v("When injecting without specifying the Goner's GonerId, it's termed as anonymous injection, indicated by the injection tag "),n("code",[t._v('gone:"*"')]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("In anonymous injection, if there are multiple compatible types of "),n("strong",[t._v("Goner")]),t._v(", only one will be injected, typically the first one revived. This may lead to undefined behavior. For example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n\n\tseller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// anonymous injection, if multiple Workers exist, only one will be injected, typically the first one")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"John"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Alice"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("h3",{attrs:{id:"named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#named-injection"}},[t._v("#")]),t._v(" Named Injection")]),t._v(" "),n("p",[t._v("When injecting with a specified GonerId, it's termed as named injection. Named injection accurately injects a Goner based on its name, which is useful in practical development. It's essential to ensure that a Goner with the corresponding GonerId is buried in the cemetery.")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, embedding `gone.Flag` anonymously indicates that this structure is a Goner")]),t._v("\n Manager "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// named injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"supported-injection-property-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#supported-injection-property-types"}},[t._v("#")]),t._v(" Supported Injection Property Types")]),t._v(" "),n("h3",{attrs:{id:"_1-pointer-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-pointer-types"}},[t._v("#")]),t._v(" 1. Pointer Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a pointer, it's termed as "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_2-value-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-value-types"}},[t._v("#")]),t._v(" 2. Value Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a value type, it's termed as "),n("strong",[t._v("value injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v('In Go, value types are passed by copy, meaning that when using value injection, a new "object" is created, and the old and new objects are independent in memory. This may lead to unexpected results. For example:')]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value injection")]),t._v("\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// value injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the above code, the printed result will be "),n("code",[t._v("false")]),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"_3-interface-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-interface-types"}},[t._v("#")]),t._v(" 3. Interface Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is an interface type, it's termed as "),n("strong",[t._v("interface injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// interface type")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_4-slice-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-slice-types"}},[t._v("#")]),t._v(" 4. Slice Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a slice type, it's termed as "),n("strong",[t._v("slice injection")]),t._v('. In slice injection, the element types of the slice can be value types, pointer types, or interface types. Due to the "copy by pass" nature of value types, it\'s not recommended to use them.')]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as interface types")]),t._v("\n workers "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as pointer types")]),t._v("\n persons "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as value types (not recommended)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_5-map-types"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-map-types"}},[t._v("#")]),t._v(" 5. Map Types")]),t._v(" "),n("p",[t._v("When the injected Goner property is a map type, it's termed as "),n("strong",[t._v("map injection")]),t._v('. In map injection, the element types of the map can be value types, pointer types, or interface types. Due to the "copy by pass" nature of value types, it\'s not recommended to use them. The keys of the map are always of type '),n("code",[t._v("string")]),t._v(", and the value of each key is the GonerId of the injected Goner.")]),t._v(" "),n("p",[n("strong",[t._v("Example:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as interface types")]),t._v("\n workers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as pointer types")]),t._v("\n persons "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as value types (not recommended)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"pointer-injection-vs-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection vs Interface Injection")]),t._v(" "),n("p",[t._v("During the Bury process of a Goner in Gone, it's required to pass a reference. Both pointer injection and interface injection can pass the reference obtained from the "),n("code",[t._v("Cemetery.Bury")]),t._v(" method to the properties of a structure. "),n("strong",[t._v("Pointer injection")]),t._v(" is simple and straightforward, with a one-to-one correspondence between types, requiring no further explanation. "),n("strong",[t._v("Interfaces")]),t._v(" in Go are designed to decouple business usage from business logic implementation, allowing the usage side to be unaware of the implementation details. Interfaces also help break circular dependencies, which are not allowed in Go due to its strong typing and compile-time checks. Here's how interfaces can be used to break circular dependencies between packages:")]),t._v(" "),n("p",[t._v("Circular dependency:\n"),n("img",{attrs:{src:s(268),alt:"Circular Dependency"}})]),t._v(" "),n("p",[t._v("Using interfaces to break the circular dependency:\n"),n("img",{attrs:{src:s(269),alt:"Using Interfaces to Break Circular Dependency"}})]),t._v(" "),n("p",[t._v('Using interfaces helps hide implementation details, reduces coupling between modules, and adheres better to the "open-closed" principle. Therefore, '),n("strong",[t._v("interface injection")]),t._v(" is recommended. However, there are no absolute rules, and introducing interfaces will increase additional costs. Hence, we still support "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"slice-injection-vs-map-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#slice-injection-vs-map-injection"}},[t._v("#")]),t._v(" Slice Injection vs Map Injection")]),t._v(" "),n("p",[t._v("Gone supports injecting into "),n("code",[t._v("Slice")]),t._v(" and "),n("code",[t._v("Map")]),t._v(", termed as "),n("strong",[t._v("slice injection")]),t._v(" and "),n("strong",[t._v("map injection")]),t._v(" respectively. In both cases, the elements of the Slice and Map can be value types, pointer types, or interface types. However, due to the \"copy by pass\" nature of value types, it's not recommended to use them. Here's how you can inject into Slice and Map properties:")]),t._v(" "),n("p",[n("strong",[t._v("Injection rules are as follows:")])]),t._v(" "),n("ul",[n("li",[t._v("Slice and Map can contain elements of Goner pointer types, Goner value types, or interfaces.")]),t._v(" "),n("li",[t._v("Gone injects all compatible types of Goners into Slice and Map.")]),t._v(" "),n("li",[t._v("Map keys are of type "),n("code",[t._v("string")]),t._v(", and the value of each key is the GonerId of the injected Goner. If no GonerId is specified during burial, Gone generates one.")])]),t._v(" "),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("Using value types as Slice and Map types is not recommended.")])]),t._v(" "),n("p",[t._v("Here's a complete example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as Goner pointer types")]),t._v("\n\taSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Slice type with elements as Goner value types")]),t._v("\n\n\taMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as Goner pointer types")]),t._v("\n\taMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Map type with values as Goner value types")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 constructs A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 constructs A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The example produces the following output:\n"),n("img",{attrs:{src:s(278),alt:"slice map injected example result"}})])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/2.f18e2b4c.js b/assets/js/2.accce834.js similarity index 92% rename from assets/js/2.f18e2b4c.js rename to assets/js/2.accce834.js index 6ea6c34..6a843eb 100644 --- a/assets/js/2.f18e2b4c.js +++ b/assets/js/2.accce834.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[2],{248:function(t,e,a){},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,a){},252:function(t,e,a){},253:function(t,e,a){},255:function(t,e,a){"use strict";a(248)},258:function(t,e,a){"use strict";a.r(e);var n=a(271),s=a(260),i=a(242);function r(t,e){if("group"===e.type){const a=e.path&&Object(i.e)(t,e.path),n=e.children.some(e=>"group"===e.type?r(t,e):"page"===e.type&&Object(i.e)(t,e.path));return a||n}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:n.default,SidebarLink:s.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let a=0;a-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(i.e)(this.$route,t.regularPath)}}},l=a(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(a,n){return e("li",{key:n},["group"===a.type?e("SidebarGroup",{attrs:{item:a,open:n===t.openGroupIndex,collapsable:a.collapsable||a.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(n)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:a}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},260:function(t,e,a){"use strict";a.r(e);var n=a(242);function s(t,e,a,n,s){const i={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:n,"sidebar-link":!0}};return s>2&&(i.style={"padding-left":s+"rem"}),t("RouterLink",i,a)}function i(t,e,a,r,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(n.e)(r,a+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[s(t,a+"#"+e.slug,e.title,u,e.level-1),i(t,e.children,a,r,o,l+1)])}))}var r={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:a,$route:r,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(n.e)(r,u.path),d="auto"===u.type?p||u.children.some(t=>Object(n.e)(r,u.basePath+"#"+t.slug)):p,h="external"===u.type?function(t,e,a){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[a,t("OutboundLink")])}(t,u.path,u.title||u.path):s(t,u.path,u.title||u.path,d),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[h,i(t,u.children,u.basePath,r,f)];if((d||b)&&u.headers&&!n.d.test(u.path)){return[h,i(t,Object(n.c)(u.headers),u.path,r,f)]}return h}},o=(a(255),a(10)),l=Object(o.a)(r,void 0,void 0,!1,null,null,null);e.default=l.exports},261:function(t,e,a){},264:function(t,e,a){"use strict";a(251)},265:function(t,e,a){var n=a(12),s=a(4),i=a(11);t.exports=function(t){return"string"==typeof t||!s(t)&&i(t)&&"[object String]"==n(t)}},266:function(t,e,a){"use strict";a(252)},267:function(t,e,a){},268:function(t,e,a){"use strict";a(253)},269:function(t,e,a){},271:function(t,e,a){"use strict";a.r(e);var n=a(242),s={name:"SidebarGroup",components:{DropdownTransition:a(245).default},props:["item","open","collapsable","depth"],beforeCreate(){this.$options.components.SidebarLinks=a(258).default},methods:{isActive:n.e}},i=(a(268),a(10)),r=Object(i.a)(s,(function(){var t=this,e=t._self._c;return e("section",{staticClass:"sidebar-group",class:[{collapsable:t.collapsable,"is-sub-group":0!==t.depth},"depth-"+t.depth]},[t.item.path?e("RouterLink",{staticClass:"sidebar-heading clickable",class:{open:t.open,active:t.isActive(t.$route,t.item.path)},attrs:{to:t.item.path},nativeOn:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]):e("p",{staticClass:"sidebar-heading",class:{open:t.open},on:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]),t._v(" "),e("DropdownTransition",[t.open||!t.collapsable?e("SidebarLinks",{staticClass:"sidebar-group-items",attrs:{items:t.item.children,"sidebar-depth":t.item.sidebarDepth,"initial-open-group-index":t.item.initialOpenGroupIndex,depth:t.depth+1}}):t._e()],1)],1)}),[],!1,null,null,null);e.default=r.exports},272:function(t,e,a){"use strict";a.r(e);var n=a(249),s=a.n(n),i=a(242),r={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=s()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:a="",docsBranch:n="master",docsRepo:i=e}=this.$site.themeConfig;return t&&i&&this.$page.relativePath?this.createEditLink(e,i,a,n,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,a,n,s){if(/bitbucket.org/.test(e)){return e.replace(i.a,"")+"/src"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s+`?mode=edit&spa=0&at=${n}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(i.a,"")+"/-/edit"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s}return(i.i.test(e)?e:"https://github.com/"+e).replace(i.a,"")+"/edit"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s}}},o=(a(264),a(10)),l=Object(o.a)(r,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=l.exports},273:function(t,e,a){"use strict";a.r(e);a(91);var n=a(242),s=a(265),i=a.n(s),r=a(249),o=a.n(r),l={name:"PageNav",props:["sidebarItems"],computed:{prev(){return c(u.PREV,this)},next(){return c(u.NEXT,this)}}};const u={NEXT:{resolveLink:function(t,e){return p(t,e,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,e){return p(t,e,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function c(t,{$themeConfig:e,$page:a,$route:s,$site:r,sidebarItems:l}){const{resolveLink:u,getThemeLinkConfig:c,getPageLinkConfig:p}=t,d=c(e),h=p(a),f=o()(h)?d:h;return!1===f?void 0:i()(f)?Object(n.k)(r.pages,f,s.path):u(a,l)}function p(t,e,a){const n=[];!function t(e,a){for(let n=0,s=e.length;n({isSidebarOpen:!1}),computed:{shouldShowNavbar(){const{themeConfig:t}=this.$site,{frontmatter:e}=this.$page;return!1!==e.navbar&&!1!==t.navbar&&(this.$title||t.logo||t.repo||t.nav||this.$themeLocaleConfig.nav)},shouldShowSidebar(){const{frontmatter:t}=this.$page;return!t.home&&!1!==t.sidebar&&this.sidebarItems.length},sidebarItems(){return Object(o.l)(this.$page,this.$page.regularPath,this.$site,this.$localePath)},pageClasses(){const t=this.$page.frontmatter.pageClass;return[{"no-navbar":!this.shouldShowNavbar,"sidebar-open":this.isSidebarOpen,"no-sidebar":!this.shouldShowSidebar},t]}},mounted(){this.$router.afterEach(()=>{this.isSidebarOpen=!1})},methods:{toggleSidebar(t){this.isSidebarOpen="boolean"==typeof t?t:!this.isSidebarOpen,this.$emit("toggle-sidebar",this.isSidebarOpen)},onTouchStart(t){this.touchStart={x:t.changedTouches[0].clientX,y:t.changedTouches[0].clientY}},onTouchEnd(t){const e=t.changedTouches[0].clientX-this.touchStart.x,a=t.changedTouches[0].clientY-this.touchStart.y;Math.abs(e)>Math.abs(a)&&Math.abs(e)>40&&(e>0&&this.touchStart.x<=80?this.toggleSidebar(!0):this.toggleSidebar(!1))}}},u=a(10),c=Object(u.a)(l,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"theme-container",class:t.pageClasses,on:{touchstart:t.onTouchStart,touchend:t.onTouchEnd}},[t.shouldShowNavbar?e("Navbar",{on:{"toggle-sidebar":t.toggleSidebar}}):t._e(),t._v(" "),e("div",{staticClass:"sidebar-mask",on:{click:function(e){return t.toggleSidebar(!1)}}}),t._v(" "),e("Sidebar",{attrs:{items:t.sidebarItems},on:{"toggle-sidebar":t.toggleSidebar},scopedSlots:t._u([{key:"top",fn:function(){return[t._t("sidebar-top")]},proxy:!0},{key:"bottom",fn:function(){return[t._t("sidebar-bottom")]},proxy:!0}],null,!0)}),t._v(" "),t.$page.frontmatter.home?e("Home"):e("Page",{attrs:{"sidebar-items":t.sidebarItems},scopedSlots:t._u([{key:"top",fn:function(){return[t._t("page-top")]},proxy:!0},{key:"bottom",fn:function(){return[t._t("page-bottom")]},proxy:!0}],null,!0)})],1)}),[],!1,null,null,null);e.default=c.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[2],{248:function(t,e,a){},249:function(t,e){t.exports=function(t){return null==t}},251:function(t,e,a){},252:function(t,e,a){},253:function(t,e,a){},255:function(t,e,a){"use strict";a(248)},256:function(t,e,a){"use strict";a.r(e);var n=a(271),s=a(258),i=a(242);function r(t,e){if("group"===e.type){const a=e.path&&Object(i.e)(t,e.path),n=e.children.some(e=>"group"===e.type?r(t,e):"page"===e.type&&Object(i.e)(t,e.path));return a||n}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:n.default,SidebarLink:s.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let a=0;a-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(i.e)(this.$route,t.regularPath)}}},l=a(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(a,n){return e("li",{key:n},["group"===a.type?e("SidebarGroup",{attrs:{item:a,open:n===t.openGroupIndex,collapsable:a.collapsable||a.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(n)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:a}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},258:function(t,e,a){"use strict";a.r(e);var n=a(242);function s(t,e,a,n,s){const i={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:n,"sidebar-link":!0}};return s>2&&(i.style={"padding-left":s+"rem"}),t("RouterLink",i,a)}function i(t,e,a,r,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(n.e)(r,a+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[s(t,a+"#"+e.slug,e.title,u,e.level-1),i(t,e.children,a,r,o,l+1)])}))}var r={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:a,$route:r,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(n.e)(r,u.path),d="auto"===u.type?p||u.children.some(t=>Object(n.e)(r,u.basePath+"#"+t.slug)):p,h="external"===u.type?function(t,e,a){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[a,t("OutboundLink")])}(t,u.path,u.title||u.path):s(t,u.path,u.title||u.path,d),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[h,i(t,u.children,u.basePath,r,f)];if((d||b)&&u.headers&&!n.d.test(u.path)){return[h,i(t,Object(n.c)(u.headers),u.path,r,f)]}return h}},o=(a(255),a(10)),l=Object(o.a)(r,void 0,void 0,!1,null,null,null);e.default=l.exports},259:function(t,e,a){},262:function(t,e,a){"use strict";a(251)},263:function(t,e,a){var n=a(12),s=a(4),i=a(11);t.exports=function(t){return"string"==typeof t||!s(t)&&i(t)&&"[object String]"==n(t)}},264:function(t,e,a){"use strict";a(252)},265:function(t,e,a){},266:function(t,e,a){"use strict";a(253)},267:function(t,e,a){},271:function(t,e,a){"use strict";a.r(e);var n=a(242),s={name:"SidebarGroup",components:{DropdownTransition:a(245).default},props:["item","open","collapsable","depth"],beforeCreate(){this.$options.components.SidebarLinks=a(256).default},methods:{isActive:n.e}},i=(a(266),a(10)),r=Object(i.a)(s,(function(){var t=this,e=t._self._c;return e("section",{staticClass:"sidebar-group",class:[{collapsable:t.collapsable,"is-sub-group":0!==t.depth},"depth-"+t.depth]},[t.item.path?e("RouterLink",{staticClass:"sidebar-heading clickable",class:{open:t.open,active:t.isActive(t.$route,t.item.path)},attrs:{to:t.item.path},nativeOn:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]):e("p",{staticClass:"sidebar-heading",class:{open:t.open},on:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]),t._v(" "),e("DropdownTransition",[t.open||!t.collapsable?e("SidebarLinks",{staticClass:"sidebar-group-items",attrs:{items:t.item.children,"sidebar-depth":t.item.sidebarDepth,"initial-open-group-index":t.item.initialOpenGroupIndex,depth:t.depth+1}}):t._e()],1)],1)}),[],!1,null,null,null);e.default=r.exports},272:function(t,e,a){"use strict";a.r(e);var n=a(249),s=a.n(n),i=a(242),r={name:"PageEdit",computed:{lastUpdated(){return this.$page.lastUpdated},lastUpdatedText(){return"string"==typeof this.$themeLocaleConfig.lastUpdated?this.$themeLocaleConfig.lastUpdated:"string"==typeof this.$site.themeConfig.lastUpdated?this.$site.themeConfig.lastUpdated:"Last Updated"},editLink(){const t=s()(this.$page.frontmatter.editLink)?this.$site.themeConfig.editLinks:this.$page.frontmatter.editLink,{repo:e,docsDir:a="",docsBranch:n="master",docsRepo:i=e}=this.$site.themeConfig;return t&&i&&this.$page.relativePath?this.createEditLink(e,i,a,n,this.$page.relativePath):null},editLinkText(){return this.$themeLocaleConfig.editLinkText||this.$site.themeConfig.editLinkText||"Edit this page"}},methods:{createEditLink(t,e,a,n,s){if(/bitbucket.org/.test(e)){return e.replace(i.a,"")+"/src"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s+`?mode=edit&spa=0&at=${n}&fileviewer=file-view-default`}if(/gitlab.com/.test(e)){return e.replace(i.a,"")+"/-/edit"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s}return(i.i.test(e)?e:"https://github.com/"+e).replace(i.a,"")+"/edit"+`/${n}/`+(a?a.replace(i.a,"")+"/":"")+s}}},o=(a(262),a(10)),l=Object(o.a)(r,(function(){var t=this,e=t._self._c;return e("footer",{staticClass:"page-edit"},[t.editLink?e("div",{staticClass:"edit-link"},[e("a",{attrs:{href:t.editLink,target:"_blank",rel:"noopener noreferrer"}},[t._v(t._s(t.editLinkText))]),t._v(" "),e("OutboundLink")],1):t._e(),t._v(" "),t.lastUpdated?e("div",{staticClass:"last-updated"},[e("span",{staticClass:"prefix"},[t._v(t._s(t.lastUpdatedText)+":")]),t._v(" "),e("span",{staticClass:"time"},[t._v(t._s(t.lastUpdated))])]):t._e()])}),[],!1,null,null,null);e.default=l.exports},273:function(t,e,a){"use strict";a.r(e);a(91);var n=a(242),s=a(263),i=a.n(s),r=a(249),o=a.n(r),l={name:"PageNav",props:["sidebarItems"],computed:{prev(){return c(u.PREV,this)},next(){return c(u.NEXT,this)}}};const u={NEXT:{resolveLink:function(t,e){return p(t,e,1)},getThemeLinkConfig:({nextLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.next},PREV:{resolveLink:function(t,e){return p(t,e,-1)},getThemeLinkConfig:({prevLinks:t})=>t,getPageLinkConfig:({frontmatter:t})=>t.prev}};function c(t,{$themeConfig:e,$page:a,$route:s,$site:r,sidebarItems:l}){const{resolveLink:u,getThemeLinkConfig:c,getPageLinkConfig:p}=t,d=c(e),h=p(a),f=o()(h)?d:h;return!1===f?void 0:i()(f)?Object(n.k)(r.pages,f,s.path):u(a,l)}function p(t,e,a){const n=[];!function t(e,a){for(let n=0,s=e.length;n({isSidebarOpen:!1}),computed:{shouldShowNavbar(){const{themeConfig:t}=this.$site,{frontmatter:e}=this.$page;return!1!==e.navbar&&!1!==t.navbar&&(this.$title||t.logo||t.repo||t.nav||this.$themeLocaleConfig.nav)},shouldShowSidebar(){const{frontmatter:t}=this.$page;return!t.home&&!1!==t.sidebar&&this.sidebarItems.length},sidebarItems(){return Object(o.l)(this.$page,this.$page.regularPath,this.$site,this.$localePath)},pageClasses(){const t=this.$page.frontmatter.pageClass;return[{"no-navbar":!this.shouldShowNavbar,"sidebar-open":this.isSidebarOpen,"no-sidebar":!this.shouldShowSidebar},t]}},mounted(){this.$router.afterEach(()=>{this.isSidebarOpen=!1})},methods:{toggleSidebar(t){this.isSidebarOpen="boolean"==typeof t?t:!this.isSidebarOpen,this.$emit("toggle-sidebar",this.isSidebarOpen)},onTouchStart(t){this.touchStart={x:t.changedTouches[0].clientX,y:t.changedTouches[0].clientY}},onTouchEnd(t){const e=t.changedTouches[0].clientX-this.touchStart.x,a=t.changedTouches[0].clientY-this.touchStart.y;Math.abs(e)>Math.abs(a)&&Math.abs(e)>40&&(e>0&&this.touchStart.x<=80?this.toggleSidebar(!0):this.toggleSidebar(!1))}}},u=a(10),c=Object(u.a)(l,(function(){var t=this,e=t._self._c;return e("div",{staticClass:"theme-container",class:t.pageClasses,on:{touchstart:t.onTouchStart,touchend:t.onTouchEnd}},[t.shouldShowNavbar?e("Navbar",{on:{"toggle-sidebar":t.toggleSidebar}}):t._e(),t._v(" "),e("div",{staticClass:"sidebar-mask",on:{click:function(e){return t.toggleSidebar(!1)}}}),t._v(" "),e("Sidebar",{attrs:{items:t.sidebarItems},on:{"toggle-sidebar":t.toggleSidebar},scopedSlots:t._u([{key:"top",fn:function(){return[t._t("sidebar-top")]},proxy:!0},{key:"bottom",fn:function(){return[t._t("sidebar-bottom")]},proxy:!0}],null,!0)}),t._v(" "),t.$page.frontmatter.home?e("Home"):e("Page",{attrs:{"sidebar-items":t.sidebarItems},scopedSlots:t._u([{key:"top",fn:function(){return[t._t("page-top")]},proxy:!0},{key:"bottom",fn:function(){return[t._t("page-bottom")]},proxy:!0}],null,!0)})],1)}),[],!1,null,null,null);e.default=c.exports}}]); \ No newline at end of file diff --git a/assets/js/20.ee1d0b8e.js b/assets/js/20.5e2ab842.js similarity index 99% rename from assets/js/20.ee1d0b8e.js rename to assets/js/20.5e2ab842.js index dbd15f9..b90c9ec 100644 --- a/assets/js/20.ee1d0b8e.js +++ b/assets/js/20.5e2ab842.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[20],{256:function(t,s){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},257:function(t,s,a){t.exports=a.p+"assets/img/image7.ca08b1e7.png"},278:function(t,s,a){t.exports=a.p+"assets/img/image8.5d2d558a.png"},352:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone支持哪些方式注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone支持哪些方式注入"}},[t._v("#")]),t._v(" Gone支持哪些方式注入?")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#共有属性注入-vs-私有属性注入"}},[t._v("共有属性注入 vs 私有属性注入")])]),s("li",[s("a",{attrs:{href:"#匿名注入-vs-具名注入"}},[t._v("匿名注入 vs 具名注入")]),s("ul",[s("li",[s("a",{attrs:{href:"#匿名埋葬-和-具名埋葬"}},[t._v("匿名埋葬 和 具名埋葬")])]),s("li",[s("a",{attrs:{href:"#匿名注入"}},[t._v("匿名注入")])]),s("li",[s("a",{attrs:{href:"#具名注入"}},[t._v("具名注入")])])])]),s("li",[s("a",{attrs:{href:"#支持注入的属性类型"}},[t._v("支持注入的属性类型")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-指针类型"}},[t._v("1.指针类型")])]),s("li",[s("a",{attrs:{href:"#_2-值类型"}},[t._v("2.值类型")])]),s("li",[s("a",{attrs:{href:"#_3-接口类型"}},[t._v("3.接口类型")])]),s("li",[s("a",{attrs:{href:"#_4-slice-类型"}},[t._v("4.Slice 类型")])]),s("li",[s("a",{attrs:{href:"#_5-map-类型"}},[t._v("5.Map 类型")])]),s("li",[s("a",{attrs:{href:"#指针注入-vs-接口注入"}},[t._v("指针注入 vs 接口注入")])]),s("li",[s("a",{attrs:{href:"#slice注入-vs-map注入"}},[t._v("Slice注入 vs Map注入")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"共有属性注入-vs-私有属性注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#共有属性注入-vs-私有属性注入"}},[t._v("#")]),t._v(" 共有属性注入 vs 私有属性注入")]),t._v(" "),s("p",[t._v("在Gone中,及支持共有属性注入也支持私有属性注入。依赖注入,一般情况依赖注入的“对象”都只应该局限于模块内部使用,基于“开放封闭”原则,我推荐使用私有属性注入。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("推荐私有属性注入")])]),t._v(" "),s("p",[t._v("举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//属性首字母为小写,为私有属性注入,只能包内部访问")]),t._v("\n Manager "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//属性首字母为大写,为公有属性注入,访问不受限制")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"匿名注入-vs-具名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名注入-vs-具名注入"}},[t._v("#")]),t._v(" 匿名注入 vs 具名注入")]),t._v(" "),s("h3",{attrs:{id:"匿名埋葬-和-具名埋葬"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名埋葬-和-具名埋葬"}},[t._v("#")]),t._v(" 匿名埋葬 和 具名埋葬")]),t._v(" "),s("p",[t._v("需要注入一个Goner,必须先要将其载入到系统,在gone中,我们称之为"),s("strong",[t._v("埋葬")]),t._v("(也就是"),s("strong",[t._v("Bury")]),t._v("),就是在"),s("strong",[t._v("Priest")]),t._v("函数中,调用"),s("code",[t._v("cemetery.Bury")]),t._v("函数将Goner的引用“埋葬”到"),s("strong",[t._v("墓园(cemetery)")]),t._v("。埋葬时,如果不指定了GonerId,就是"),s("strong",[t._v("匿名埋葬")]),t._v(";如果指定了就是"),s("strong",[t._v("具名埋葬")]),t._v("。")]),t._v(" "),s("p",[s("strong",[s("code",[t._v("Cemetery.Bury")])]),t._v(" 函数的完整定义是这样的 "),s("strong",[s("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(",这样的定义有两层考虑:")]),t._v(" "),s("ol",[s("li",[t._v("用于支持"),s("strong",[t._v("具名埋葬")]),t._v(",第二个参数是可选的,允许传入一个字符串作为"),s("strong",[t._v("Goner")]),t._v("的"),s("strong",[t._v("ID")]),t._v("("),s("strong",[t._v("GonerId")]),t._v(");")]),t._v(" "),s("li",[t._v("使Bury函数可以支持链式调用。")])]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名埋葬第一个AGoner,ID=A1")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名埋葬第二个AGoner,ID=A2")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名埋葬")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("实际上,匿名埋葬的Goner也会被Gone随机分配一个GoneId。")])]),t._v(" "),s("h3",{attrs:{id:"匿名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名注入"}},[t._v("#")]),t._v(" 匿名注入")]),t._v(" "),s("p",[t._v("注入时,没有指明被注入Goner的GonerId就是匿名注入,即注入标签为"),s("code",[t._v('gone:"*"')]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("匿名注入时,如果存在 多个 兼容类型的"),s("strong",[t._v("Goner")]),t._v(",被注入只会是其中一个,通常是最先被 复活(Revive)的那个,可能会导致未定义行为。举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n\tseller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入,如果有存在多个Worker,则注入其中一个,通常是第一个")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"小王"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"小张"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),s("h3",{attrs:{id:"具名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#具名注入"}},[t._v("#")]),t._v(" 具名注入")]),t._v(" "),s("p",[t._v("注入时,指明了Goner的GonerId,就是具名注入;具名注入能够根据名字准确的帮我们注入一个Goner,在实际开发中很有用。需要注意的是:")]),t._v(" "),s("ol",[s("li",[t._v("需要确保存在响应GonerId的Goner被埋葬到了墓园;")]),t._v(" "),s("li",[t._v("因为go是强类型的,所以无论 匿名注入 还是 具名注入 的 Goner 都必须是类型兼容的,否则注入失败。")])]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n Manager "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"支持注入的属性类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的属性类型"}},[t._v("#")]),t._v(" 支持注入的属性类型")]),t._v(" "),s("h3",{attrs:{id:"_1-指针类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-指针类型"}},[t._v("#")]),t._v(" 1.指针类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个指针时,我们也称之为"),s("strong",[t._v("指针注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-值类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-值类型"}},[t._v("#")]),t._v(" 2.值类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个值类型时,我们也称之为"),s("strong",[t._v("值注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("“在go语言中,"),s("strong",[t._v("值类型")]),t._v(" 的赋值和传参都是传递的拷贝”,这意味着我们如果使用 "),s("strong",[t._v("值类型")]),t._v(" 注入时,实际上产生了一个新的“对象”,并且新旧对象只有在“传递那一刻”是相等,他们在内存中是独立的;这可能导致一些不符合“直觉”的结果,举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n\ta2 AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])]),s("p",[t._v("在上面的代码中,打印的结果会是 "),s("code",[t._v("false")]),t._v("。")])]),t._v(" "),s("h3",{attrs:{id:"_3-接口类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-接口类型"}},[t._v("#")]),t._v(" 3.接口类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个接口类型时,我们也称之为"),s("strong",[t._v("接口注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//接口类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-slice-类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-slice-类型"}},[t._v("#")]),t._v(" 4.Slice 类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个Slice类型时,我们也称之为"),s("strong",[t._v("Slice注入")]),t._v(";在Slice注入中,Slice的元素类型允许为 值类型、指针类型 和 接口类型;由于值类型的“拷贝传递”,我们不推荐使用。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为接口类型")]),t._v("\n workers "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为指针类型")]),t._v("\n persons "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_5-map-类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_5-map-类型"}},[t._v("#")]),t._v(" 5.Map 类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个Map类型时,我们也称之为"),s("strong",[t._v("Map注入")]),t._v(";在Map注入中,Map的元素类型允许为 值类型、指针类型 和 接口类型;由于值类型的“拷贝传递”,我们不推荐使用;Map的key只能为"),s("code",[t._v("string")]),t._v("类型,被注入后map key的值为GonerId。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为接口类型")]),t._v("\n workers "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为指针类型")]),t._v("\n persons "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"指针注入-vs-接口注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#指针注入-vs-接口注入"}},[t._v("#")]),t._v(" 指针注入 vs 接口注入")]),t._v(" "),s("p",[t._v("在Goner的Bury过程中,要求传递的是一个引用,即"),s("code",[t._v("Cemetery.Bury")]),t._v("方法的第一个参数必须是引用类型。指针注入和接口注入都可以将 "),s("strong",[t._v("Bury")]),t._v(" 的引用传递给结构体的属性。"),s("strong",[t._v("指针注入")]),t._v(",简单直观,类型间一一对应,没有什么过多需要讲解的。接口(interface)做为go语言中最精华的设计之一,语言设计之初目的就在于 "),s("strong",[t._v("业务使用方和业务实现逻辑的解耦")]),t._v(",让接口的使用方不用关注实现的细节。接口的另一个作用是解除"),s("strong",[t._v("循环依赖")]),t._v(",如果两个模块间存在循环引用并且他们位于不同的包中;这样会导致package的循环依赖,在go语言中是禁止这样的行为的,编译阶段会失败。我们可以如下使用接口来解出package间的循环依赖。")]),t._v(" "),s("p",[t._v("循环依赖:"),s("br"),t._v(" "),s("img",{attrs:{src:a(256),alt:"循环依赖"}})]),t._v(" "),s("p",[t._v("使用接口解除循环依赖:"),s("br"),t._v(" "),s("img",{attrs:{src:a(257),alt:"使用接口解除循环依赖"}})]),t._v(" "),s("p",[t._v("使用接口能够隐藏业务逻辑的实现细节,能够有效的降低模块间的耦合,也更好的遵守“开放封闭”原则;因此,我们推荐使用 "),s("strong",[t._v("接口注入")]),t._v("。但是万事没有绝对,引入接口一定会增加额外的成本,所以我们还是支持了 "),s("strong",[t._v("指针注入")]),t._v("。")]),t._v(" "),s("h3",{attrs:{id:"slice注入-vs-map注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#slice注入-vs-map注入"}},[t._v("#")]),t._v(" Slice注入 vs Map注入")]),t._v(" "),s("p",[t._v("Gone 支持对"),s("code",[t._v("Slice")]),t._v("和"),s("code",[t._v("Map")]),t._v("进行注入,即支持如下写法的:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("strong",[t._v("注入的规则如下:")])]),t._v(" "),s("ul",[s("li",[s("ol",[s("li",[t._v("Slice 和 Map 的元素类型可以是 Goner指针类型 和 Goner的值类型,也可以是一个接口;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"2"}},[s("li",[t._v("Gone会将所有类型兼容的Goner注入到Slice 和 Map;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"3"}},[s("li",[t._v("Map key的类型只能是string;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"4"}},[s("li",[t._v("Map key的值为被注入Goner的GonerId,埋葬时没有指定GonerId的匿名Goner,Gone会自动生成一个Id。")])])])]),t._v(" "),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("不推荐使用值作为Slice和Map的类型。")])]),t._v(" "),s("p",[t._v("下面是完整的例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 构造A1 AGoner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 构造A2 AGoner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("例子执行的结果如下:\n"),s("img",{attrs:{src:a(278),alt:"slice map injected example result"}})])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[20],{268:function(t,s){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},269:function(t,s,a){t.exports=a.p+"assets/img/image7.ca08b1e7.png"},278:function(t,s,a){t.exports=a.p+"assets/img/image8.5d2d558a.png"},344:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone支持哪些方式注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone支持哪些方式注入"}},[t._v("#")]),t._v(" Gone支持哪些方式注入?")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#公有属性注入-vs-私有属性注入"}},[t._v("公有属性注入 vs 私有属性注入")])]),s("li",[s("a",{attrs:{href:"#匿名注入-vs-具名注入"}},[t._v("匿名注入 vs 具名注入")]),s("ul",[s("li",[s("a",{attrs:{href:"#匿名埋葬-和-具名埋葬"}},[t._v("匿名埋葬 和 具名埋葬")])]),s("li",[s("a",{attrs:{href:"#匿名注入"}},[t._v("匿名注入")])]),s("li",[s("a",{attrs:{href:"#具名注入"}},[t._v("具名注入")])])])]),s("li",[s("a",{attrs:{href:"#支持注入的属性类型"}},[t._v("支持注入的属性类型")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-指针类型"}},[t._v("1.指针类型")])]),s("li",[s("a",{attrs:{href:"#_2-值类型"}},[t._v("2.值类型")])]),s("li",[s("a",{attrs:{href:"#_3-接口类型"}},[t._v("3.接口类型")])]),s("li",[s("a",{attrs:{href:"#_4-slice-类型"}},[t._v("4.Slice 类型")])]),s("li",[s("a",{attrs:{href:"#_5-map-类型"}},[t._v("5.Map 类型")])]),s("li",[s("a",{attrs:{href:"#指针注入-vs-接口注入"}},[t._v("指针注入 vs 接口注入")])]),s("li",[s("a",{attrs:{href:"#slice注入-vs-map注入"}},[t._v("Slice注入 vs Map注入")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"公有属性注入-vs-私有属性注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#公有属性注入-vs-私有属性注入"}},[t._v("#")]),t._v(" 公有属性注入 vs 私有属性注入")]),t._v(" "),s("p",[t._v("在Gone中,及支持共有属性注入也支持私有属性注入。依赖注入,一般情况依赖注入的“对象”都只应该局限于模块内部使用,基于“开放封闭”原则,我推荐使用私有属性注入。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("推荐私有属性注入")])]),t._v(" "),s("p",[t._v("举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//属性首字母为小写,为私有属性注入,只能包内部访问")]),t._v("\n Manager "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//属性首字母为大写,为公有属性注入,访问不受限制")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"匿名注入-vs-具名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名注入-vs-具名注入"}},[t._v("#")]),t._v(" 匿名注入 vs 具名注入")]),t._v(" "),s("h3",{attrs:{id:"匿名埋葬-和-具名埋葬"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名埋葬-和-具名埋葬"}},[t._v("#")]),t._v(" 匿名埋葬 和 具名埋葬")]),t._v(" "),s("p",[t._v("需要注入一个Goner,必须先要将其载入到系统,在gone中,我们称之为"),s("strong",[t._v("埋葬")]),t._v("(也就是"),s("strong",[t._v("Bury")]),t._v("),就是在"),s("strong",[t._v("Priest")]),t._v("函数中,调用"),s("code",[t._v("cemetery.Bury")]),t._v("函数将Goner的引用“埋葬”到"),s("strong",[t._v("墓园(cemetery)")]),t._v("。埋葬时,如果不指定了GonerId,就是"),s("strong",[t._v("匿名埋葬")]),t._v(";如果指定了就是"),s("strong",[t._v("具名埋葬")]),t._v("。")]),t._v(" "),s("p",[s("strong",[s("code",[t._v("Cemetery.Bury")])]),t._v(" 函数的完整定义是这样的 "),s("strong",[s("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(",这样的定义有两层考虑:")]),t._v(" "),s("ol",[s("li",[t._v("用于支持"),s("strong",[t._v("具名埋葬")]),t._v(",第二个参数是可选的,允许传入一个字符串作为"),s("strong",[t._v("Goner")]),t._v("的"),s("strong",[t._v("ID")]),t._v("("),s("strong",[t._v("GonerId")]),t._v(");")]),t._v(" "),s("li",[t._v("使Bury函数可以支持链式调用。")])]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名埋葬第一个AGoner,ID=A1")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名埋葬第二个AGoner,ID=A2")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名埋葬")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("实际上,匿名埋葬的Goner也会被Gone随机分配一个GoneId。")])]),t._v(" "),s("h3",{attrs:{id:"匿名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#匿名注入"}},[t._v("#")]),t._v(" 匿名注入")]),t._v(" "),s("p",[t._v("注入时,没有指明被注入Goner的GonerId就是匿名注入,即注入标签为"),s("code",[t._v('gone:"*"')]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("匿名注入时,如果存在 多个 兼容类型的"),s("strong",[t._v("Goner")]),t._v(",被注入只会是其中一个,通常是最先被 复活(Revive)的那个,可能会导致未定义行为。举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n\tseller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入,如果有存在多个Worker,则注入其中一个,通常是第一个")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"小王"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"小张"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),s("h3",{attrs:{id:"具名注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#具名注入"}},[t._v("#")]),t._v(" 具名注入")]),t._v(" "),s("p",[t._v("注入时,指明了Goner的GonerId,就是具名注入;具名注入能够根据名字准确的帮我们注入一个Goner,在实际开发中很有用。需要注意的是:")]),t._v(" "),s("ol",[s("li",[t._v("需要确保存在响应GonerId的Goner被埋葬到了墓园;")]),t._v(" "),s("li",[t._v("因为go是强类型的,所以无论 匿名注入 还是 具名注入 的 Goner 都必须是类型兼容的,否则注入失败。")])]),t._v(" "),s("p",[s("strong",[t._v("举个例子:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n Manager "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"manager"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"支持注入的属性类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的属性类型"}},[t._v("#")]),t._v(" 支持注入的属性类型")]),t._v(" "),s("h3",{attrs:{id:"_1-指针类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-指针类型"}},[t._v("#")]),t._v(" 1.指针类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个指针时,我们也称之为"),s("strong",[t._v("指针注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-值类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-值类型"}},[t._v("#")]),t._v(" 2.值类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个值类型时,我们也称之为"),s("strong",[t._v("值注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("“在go语言中,"),s("strong",[t._v("值类型")]),t._v(" 的赋值和传参都是传递的拷贝”,这意味着我们如果使用 "),s("strong",[t._v("值类型")]),t._v(" 注入时,实际上产生了一个新的“对象”,并且新旧对象只有在“传递那一刻”是相等,他们在内存中是独立的;这可能导致一些不符合“直觉”的结果,举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n\ta2 AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])]),s("p",[t._v("在上面的代码中,打印的结果会是 "),s("code",[t._v("false")]),t._v("。")])]),t._v(" "),s("h3",{attrs:{id:"_3-接口类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-接口类型"}},[t._v("#")]),t._v(" 3.接口类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个接口类型时,我们也称之为"),s("strong",[t._v("接口注入")]),t._v("。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n seller Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"seller"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//接口类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-slice-类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-slice-类型"}},[t._v("#")]),t._v(" 4.Slice 类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个Slice类型时,我们也称之为"),s("strong",[t._v("Slice注入")]),t._v(";在Slice注入中,Slice的元素类型允许为 值类型、指针类型 和 接口类型;由于值类型的“拷贝传递”,我们不推荐使用。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为接口类型")]),t._v("\n workers "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为指针类型")]),t._v("\n persons "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Slice 类型,元素为值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_5-map-类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_5-map-类型"}},[t._v("#")]),t._v(" 5.Map 类型")]),t._v(" "),s("p",[t._v("被注入的Goner属性是一个Map类型时,我们也称之为"),s("strong",[t._v("Map注入")]),t._v(";在Map注入中,Map的元素类型允许为 值类型、指针类型 和 接口类型;由于值类型的“拷贝传递”,我们不推荐使用;Map的key只能为"),s("code",[t._v("string")]),t._v("类型,被注入后map key的值为GonerId。")]),t._v(" "),s("p",[s("strong",[t._v("举个例子")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Seller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sell")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n sellers "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Seller "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为接口类型")]),t._v("\n workers "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为指针类型")]),t._v("\n persons "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Map 类型,元素为值类型")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"指针注入-vs-接口注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#指针注入-vs-接口注入"}},[t._v("#")]),t._v(" 指针注入 vs 接口注入")]),t._v(" "),s("p",[t._v("在Goner的Bury过程中,要求传递的是一个引用,即"),s("code",[t._v("Cemetery.Bury")]),t._v("方法的第一个参数必须是引用类型。指针注入和接口注入都可以将 "),s("strong",[t._v("Bury")]),t._v(" 的引用传递给结构体的属性。"),s("strong",[t._v("指针注入")]),t._v(",简单直观,类型间一一对应,没有什么过多需要讲解的。接口(interface)做为go语言中最精华的设计之一,语言设计之初目的就在于 "),s("strong",[t._v("业务使用方和业务实现逻辑的解耦")]),t._v(",让接口的使用方不用关注实现的细节。接口的另一个作用是解除"),s("strong",[t._v("循环依赖")]),t._v(",如果两个模块间存在循环引用并且他们位于不同的包中;这样会导致package的循环依赖,在go语言中是禁止这样的行为的,编译阶段会失败。我们可以如下使用接口来解出package间的循环依赖。")]),t._v(" "),s("p",[t._v("循环依赖:"),s("br"),t._v(" "),s("img",{attrs:{src:a(268),alt:"循环依赖"}})]),t._v(" "),s("p",[t._v("使用接口解除循环依赖:"),s("br"),t._v(" "),s("img",{attrs:{src:a(269),alt:"使用接口解除循环依赖"}})]),t._v(" "),s("p",[t._v("使用接口能够隐藏业务逻辑的实现细节,能够有效的降低模块间的耦合,也更好的遵守“开放封闭”原则;因此,我们推荐使用 "),s("strong",[t._v("接口注入")]),t._v("。但是万事没有绝对,引入接口一定会增加额外的成本,所以我们还是支持了 "),s("strong",[t._v("指针注入")]),t._v("。")]),t._v(" "),s("h3",{attrs:{id:"slice注入-vs-map注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#slice注入-vs-map注入"}},[t._v("#")]),t._v(" Slice注入 vs Map注入")]),t._v(" "),s("p",[t._v("Gone 支持对"),s("code",[t._v("Slice")]),t._v("和"),s("code",[t._v("Map")]),t._v("进行注入,即支持如下写法的:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("strong",[t._v("注入的规则如下:")])]),t._v(" "),s("ul",[s("li",[s("ol",[s("li",[t._v("Slice 和 Map 的元素类型可以是 Goner指针类型 和 Goner的值类型,也可以是一个接口;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"2"}},[s("li",[t._v("Gone会将所有类型兼容的Goner注入到Slice 和 Map;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"3"}},[s("li",[t._v("Map key的类型只能是string;")])])]),t._v(" "),s("li",[s("ol",{attrs:{start:"4"}},[s("li",[t._v("Map key的值为被注入Goner的GonerId,埋葬时没有指定GonerId的匿名Goner,Gone会自动生成一个Id。")])])])]),t._v(" "),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),s("p",[t._v("不推荐使用值作为Slice和Map的类型。")])]),t._v(" "),s("p",[t._v("下面是完整的例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 构造A1 AGoner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 构造A2 AGoner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("例子执行的结果如下:\n"),s("img",{attrs:{src:a(278),alt:"slice map injected example result"}})])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/22.5fdea428.js b/assets/js/22.e31c8c76.js similarity index 85% rename from assets/js/22.5fdea428.js rename to assets/js/22.e31c8c76.js index 8e04f09..af92db6 100644 --- a/assets/js/22.5fdea428.js +++ b/assets/js/22.e31c8c76.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[22],{263:function(t,c,n){},275:function(t,c,n){"use strict";n(263)},291:function(t,c,n){"use strict";n.r(c);n(275);var i=n(10),s=Object(i.a)({},(function(){var t=this,c=t._self._c;return c("div",{staticClass:"sidebar-button",on:{click:function(c){return t.$emit("toggle-sidebar")}}},[c("svg",{staticClass:"icon",attrs:{xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",role:"img",viewBox:"0 0 448 512"}},[c("path",{attrs:{fill:"currentColor",d:"M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"}})])])}),[],!1,null,null,null);c.default=s.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[22],{261:function(t,c,n){},275:function(t,c,n){"use strict";n(261)},291:function(t,c,n){"use strict";n.r(c);n(275);var i=n(10),s=Object(i.a)({},(function(){var t=this,c=t._self._c;return c("div",{staticClass:"sidebar-button",on:{click:function(c){return t.$emit("toggle-sidebar")}}},[c("svg",{staticClass:"icon",attrs:{xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",role:"img",viewBox:"0 0 448 512"}},[c("path",{attrs:{fill:"currentColor",d:"M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"}})])])}),[],!1,null,null,null);c.default=s.exports}}]); \ No newline at end of file diff --git a/assets/js/23.d8743355.js b/assets/js/23.fda2d1ea.js similarity index 99% rename from assets/js/23.d8743355.js rename to assets/js/23.fda2d1ea.js index c05d48d..b6fbb0f 100644 --- a/assets/js/23.d8743355.js +++ b/assets/js/23.fda2d1ea.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[23],{256:function(t,n){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},257:function(t,n,a){t.exports=a.p+"assets/img/image7.ca08b1e7.png"},322:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h2",{attrs:{id:"goner-and-dependency-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goner-and-dependency-injection"}},[t._v("#")]),t._v(" Goner and Dependency Injection")]),t._v(" "),n("h3",{attrs:{id:"definition-of-goner"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#definition-of-goner"}},[t._v("#")]),t._v(" Definition of Goner")]),t._v(" "),n("p",[t._v('In the Gone application, all components are required to be defined as Goners (i.e., structures that "inherit" '),n("code",[t._v("gone.Flag")]),t._v(', although Go does not have the concept of "inheritance" as seen in other languages; instead, it uses anonymous embedding). If a property of a Goner is tagged with the '),n("code",[t._v('gone:""')]),t._v(" tag, the Gone framework will attempt to automatically wire this property. Here's an example of defining a Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Injecting the above-defined AGoner into another Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n A "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The purpose of the gone tag is to inform Gone that this property needs to be automatically injected with a value")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In this example, both the injected and the injecting structures must be "),n("code",[t._v("Goner")]),t._v(" (i.e., structures embedded with "),n("code",[t._v("gone.Flag")]),t._v("). The "),n("code",[t._v('gone:"*"')]),t._v(" tag on the "),n("code",[t._v("A")]),t._v(" property of the "),n("code",[t._v("BGoner")]),t._v(" tells the framework that this property needs to be injected with a value.")]),t._v(" "),n("h3",{attrs:{id:"how-dependency-injection-is-achieved-in-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-dependency-injection-is-achieved-in-gone"}},[t._v("#")]),t._v(" How Dependency Injection is Achieved in Gone")]),t._v(" "),n("p",[t._v("In Java Spring, classes are annotated with "),n("code",[t._v("@Component")]),t._v(", "),n("code",[t._v("@Service")]),t._v(", etc., and during Spring startup, these specially annotated classes are automatically scanned, instantiated, and their annotated properties injected with corresponding values.")]),t._v(" "),n("p",[t._v("The reason Spring can achieve this functionality lies in a critical feature of Java: after Java code is compiled into a JAR file, all class bytecode is preserved, even for classes not directly dependent on the "),n("code",[t._v("main")]),t._v(" function. However, in Go, the compiled code is pruned, and only the code relevant to the "),n("code",[t._v("main")]),t._v(" function is retained in the binary. Therefore, if we merely define Goners, we'll find that our Goner code has been pruned.")]),t._v(" "),n("p",[t._v('How do we prevent our Goners from being pruned? The answer is simple: explicitly add all Goners to a "repository". In Gone, this repository is called the '),n("code",[t._v("Cemetery")]),t._v(". "),n("code",[t._v("Goner")]),t._v(' means "deceased", and '),n("code",[t._v("Cemetery")]),t._v(" is the graveyard used to bury (Bury) Goners. We can instantiate all Goners and add them to the Cemetery at program startup:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the above code, we see that "),n("code",[t._v("gone.Run")]),t._v(" can accept a function in the form of "),n("strong",[n("code",[t._v("func (cemetery gone.Cemetery) error")])]),t._v(". In practice, this function, which we call "),n("strong",[t._v("Priest")]),t._v(", is responsible for burying Goners in the cemetery.")]),t._v(" "),n("h3",{attrs:{id:"how-to-execute-business-code-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-execute-business-code-in-goners"}},[t._v("#")]),t._v(" How to Execute Business Code in Goners?")]),t._v(" "),n("p",[t._v("In Gone, we've made an interesting definition: when all properties of a Goner are injected, we say that this Goner has been resurrected (Revive). If you look at the source code of Gone, you'll find that the component responsible for managing the revived Goner status is called "),n("strong",[t._v("Heaven")]),t._v(", inspired by the mythical belief across religions that people ascend to heaven after death.")]),t._v(" "),n("p",[t._v("To execute business code in Goners, we can define a method on Goners called "),n("strong",[n("code",[t._v("AfterRevive() gone.AfterReviveError")])]),t._v(". This method is executed after a Goner is "),n("strong",[t._v("Revived")]),t._v(", and Goners possessing this method are called "),n("strong",[t._v("Prophets")]),t._v(". In practice, we typically only need to define a small number of prophets to guide code execution.")]),t._v(" "),n("p",[t._v("Here's an example; you can find the code "),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/after-revive/main.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is a function executed after revival.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Executing the above code will yield the following result:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Computer\nRevive main/Adder\nI want to compute"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("add")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),t._v(" is "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3000")]),t._v("\n")])])]),n("h3",{attrs:{id:"the-gone-command-automatically-generating-priest-functions"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#the-gone-command-automatically-generating-priest-functions"}},[t._v("#")]),t._v(" The gone Command: Automatically Generating "),n("code",[t._v("Priest")]),t._v(" Functions")]),t._v(" "),n("p",[t._v("In fact, we've already covered the core functionality of the Gone framework. However, due to limitations in Golang itself, we can't achieve the same level of convenience as Spring, where all "),n("strong",[n("code",[t._v("Goners")])]),t._v(" need to be manually "),n("strong",[t._v("buried")]),t._v(" (**"),n("code",[t._v("Bury")]),t._v("**ed) into the "),n("strong",[n("code",[t._v("Cemetery")])]),t._v(". To make Gone easier to use, we've developed a utility tool to automatically generate "),n("strong",[n("code",[t._v("Priest")])]),t._v(" functions. Below, we'll explain how to use this utility tool in a project.")]),t._v(" "),n("blockquote",[n("p",[t._v("You can find the complete code "),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/gen-code",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1)])]),t._v(" "),n("h4",{attrs:{id:"_1-install-the-utility-tool-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-install-the-utility-tool-gone"}},[t._v("#")]),t._v(" 1. Install the Utility Tool: gone")]),t._v(" "),n("p",[t._v("The utility tool, also named gone, can be installed using "),n("code",[t._v("go install")]),t._v(" as follows:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),n("p",[t._v("For more information on using the gone command, please refer to the "),n("a",{attrs:{href:"https://goner.fun/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone utility tool documentation"),n("OutboundLink")],1),t._v(".")]),t._v(" "),n("h4",{attrs:{id:"_2-create-a-new-project-named-gen-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-create-a-new-project-named-gen-code"}},[t._v("#")]),t._v(" 2. Create a New Project Named "),n("code",[t._v("gen-code")])]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" gen-code\n"),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" gen-code\ngo mod init gen-code\n")])])]),n("h4",{attrs:{id:"_3-create-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-create-goners"}},[t._v("#")]),t._v(" 3. Create Goners")]),t._v(" "),n("p",[t._v("File: goner.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is a function executed after revival.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, please note that we've added two factory functions "),n("code",[t._v("NewAdder() gone.Goner")]),t._v(" and "),n("code",[t._v("func NewComputer() gone.Goner")]),t._v(", and we've put a special comment before the functions:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n")])])]),n("p",[t._v("Please do not remove this comment, as it tells the utility tool how to generate the code.")]),t._v(" "),n("h4",{attrs:{id:"_4-use-the-utility-tool"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-use-the-utility-tool"}},[t._v("#")]),t._v(" 4. Use the Utility Tool")]),t._v(" "),n("p",[t._v("Please execute the following command in the "),n("code",[t._v("gen-code")]),t._v(" directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" main "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go\n")])])]),n("p",[t._v("This command scans the current directory and generates a "),n("strong",[t._v("priest")]),t._v(" function with the function name "),n("code",[t._v("Priest")]),t._v(", in the package named "),n("code",[t._v("main")]),t._v(", and puts the code in a file named "),n("code",[t._v("priest.go")]),t._v(".\nAfter executing the command, a file named "),n("code",[t._v("priest.go")]),t._v(" will be generated in the current directory with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h4",{attrs:{id:"_5-add-a-main-function"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-add-a-main-function"}},[t._v("#")]),t._v(" 5. Add a "),n("code",[t._v("main")]),t._v(" Function")]),t._v(" "),n("p",[t._v("File: main.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Now, we've completed the entire small Gone program. Its file structure is as follows:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v("\n├── go.mod\n├── go.sum\n├── goner.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Definition of goners")]),t._v("\n├── main.go\n└── priest.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Generated code")]),t._v("\n")])])]),n("p",[t._v("You can run the program using the command "),n("code",[t._v("go run .")]),t._v(", and the program will output the following:")]),t._v(" "),n("div",{staticClass:"language-code extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Adder\nRevive main/Computer\nI want to compute!\n1000 add 2000 is 3000\n")])])]),n("h2",{attrs:{id:"anonymous-injection-vs-named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("#")]),t._v(" Anonymous Injection vs Named Injection")]),t._v(" "),n("p",[t._v("In the preceding sections, all of our examples have demonstrated anonymous injection by type. When injecting anonymously, if there are multiple "),n("strong",[n("code",[t._v("Goners")])]),t._v(" of the same type, only one will be injected, typically the first one that was revived. Gone also supports named injection, or "),n("strong",[t._v("named burial")]),t._v(".")]),t._v(" "),n("p",[t._v("Firstly, let's take a look at the complete definition of the "),n("strong",[n("code",[t._v("Cemetery.Bury")])]),t._v(" function: "),n("strong",[n("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(". This definition serves two purposes:")]),t._v(" "),n("ol",[n("li",[t._v("It supports named burial, where the second parameter is optional, allowing you to pass a string as the "),n("strong",[t._v("ID")]),t._v(" ("),n("strong",[t._v("GonerId")]),t._v(") of the "),n("strong",[t._v("Goner")]),t._v(".")]),t._v(" "),n("li",[t._v("It supports chaining of the Bury function.")])]),t._v(" "),n("h3",{attrs:{id:"named-burial"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#named-burial"}},[t._v("#")]),t._v(" Named Burial")]),t._v(" "),n("p",[t._v("To achieve named burial, our code can be written like this:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burial of the first AGoner, ID=A1")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burial of the second AGoner, ID=A2")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Alternatively, you can decouple the construction of Goners from their burial like this:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 constructs A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 constructs A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Next is the named injection within structs, which can be better understood with an example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\ta "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous injection of an AGoner")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A1")]),t._v("\n\ta2 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A2")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Note: In the code above, the tag after the struct field:")]),t._v(" "),n("ul",[n("li",[n("code",[t._v('gone:"*"')]),t._v(" denotes anonymous injection.")]),t._v(" "),n("li",[n("code",[t._v('gone:"A1"')]),t._v(" denotes injection with the ID "),n("code",[t._v("A1")]),t._v(".")])]),t._v(" "),n("p",[t._v("In Go, whether anonymous or named, the "),n("strong",[t._v("Goner")]),t._v(" to be injected must be type-compatible; otherwise, the injection will fail.")]),t._v(" "),n("h2",{attrs:{id:"pointer-injection-value-injection-and-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-value-injection-and-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection, Value Injection, and Interface Injection")]),t._v(" "),n("p",[t._v("If the property of the injected struct is a pointer, then the injection is called "),n("strong",[t._v("pointer injection")]),t._v(". The definitions for "),n("strong",[t._v("value injection")]),t._v(" and "),n("strong",[t._v("interface injection")]),t._v(" are similar. Let's take an example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Speaker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\ta0 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous injection of an AGoner; pointer injection")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A1; pointer injection")]),t._v("\n\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n\ta3 Speaker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Interface injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, "),n("code",[t._v("BGoner.a0")]),t._v(" and "),n("code",[t._v("BGoner.a1")]),t._v(" are subjected to "),n("strong",[t._v("pointer injection")]),t._v(". "),n("code",[t._v("BGoner.a2")]),t._v(" is subjected to "),n("strong",[t._v("value injection")]),t._v(", while "),n("code",[t._v("BGoner.a3")]),t._v(" is subjected to "),n("strong",[t._v("interface injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("A special note:")]),t._v(' In Go, "assigning or passing a value type involves making a copy." This means that when using '),n("strong",[t._v("value injection")]),t._v(', a new "object" is actually created, and the new and old objects are only equal at the moment of transfer. They are independent in memory. This can lead to some unexpected results. For example:')]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(" are both injected with the same Goner ("),n("code",[t._v("A1")]),t._v("). However, since it's "),n("strong",[t._v("value injection")]),t._v(", during the injection process, the framework can only copy the value of "),n("code",[t._v("A1")]),t._v(" Goner to "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(". After injection, "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(" have no connection with "),n("code",[t._v("A1")]),t._v(", and there's no connection between "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(". There will also be three separate spaces in memory occupied by "),n("code",[t._v("AGoner")]),t._v(" type. The result of "),n("code",[t._v('fmt.Printf("a1 is eq a2: %v", g.a1 == g.a2)')]),t._v(" will be "),n("code",[t._v("false")]),t._v(".")]),t._v(" "),n("blockquote",[n("p",[t._v("Considering that "),n("strong",[t._v("Gone")]),t._v(" is primarily intended as a foundational framework, we have retained the "),n("strong",[t._v("value injection")]),t._v(" feature. However, in most cases, we recommend using "),n("strong",[t._v("pointer injection")]),t._v(" and "),n("strong",[t._v("interface injection")]),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"pointer-injection-vs-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection vs Interface Injection")]),t._v(" "),n("p",[t._v("During the Bury process of "),n("strong",[t._v("Goner")]),t._v(", it's required to pass a reference. This means that the first parameter of the "),n("code",[t._v("Cemetery.Bury")]),t._v(" method must be of a reference type. Both pointer injection and interface injection can pass the reference of "),n("strong",[t._v("Bury")]),t._v(" to the properties of a struct. "),n("strong",[t._v("Pointer injection")]),t._v(" is straightforward and intuitive, with a one-to-one correspondence between types, requiring little explanation.")]),t._v(" "),n("p",[t._v("Interfaces are one of the most essential designs in Go. Their purpose is to decouple the business usage from the implementation logic, allowing users of the interface to not be concerned with the implementation details. Another purpose of interfaces is to break circular dependencies. If two modules have a circular reference and they are in different packages, it violates the rule of no circular dependencies in Go, leading to compilation failure. We can use interfaces to break this circular dependency.")]),t._v(" "),n("p",[t._v("Circular dependency:\n"),n("img",{attrs:{src:a(256),alt:"Circular dependency"}})]),t._v(" "),n("p",[t._v("Using interfaces to break circular dependency:\n"),n("img",{attrs:{src:a(257),alt:"Using interfaces to break circular dependency"}})]),t._v(" "),n("p",[t._v('Using interfaces can hide the implementation details of business logic, effectively reducing the coupling between modules and better adhering to the "open-closed" principle. Therefore, we recommend using '),n("strong",[t._v("interface injection")]),t._v(". However, there are no absolutes in everything, introducing interfaces will certainly increase additional costs, so we still support "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"slice-injection-and-map-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#slice-injection-and-map-injection"}},[t._v("#")]),t._v(" Slice Injection and Map Injection")]),t._v(" "),n("p",[t._v("Gone supports injection into "),n("code",[t._v("Slice")]),t._v(" and "),n("code",[t._v("Map")]),t._v(", meaning it supports the following syntax:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner pointers")]),t._v("\n aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner values")]),t._v("\n\n aMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner pointers")]),t._v("\n aMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner values")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[n("strong",[t._v("The rules for injection are as follows:")])]),t._v(" "),n("ul",[n("li",[n("ol",[n("li",[t._v("The element type of Slice and Map can be Goner pointer type or Goner value type, or an interface.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"2"}},[n("li",[t._v("Gone will inject all types compatible with Goner into Slice and Map.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"3"}},[n("li",[t._v("The type of Map key can only be string.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"4"}},[n("li",[t._v("The value of Map key is the GonerId of the injected Goner. If no GonerId is specified when burying an anonymous Goner, Gone will generate one automatically.")])])])]),t._v(" "),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("Using values as the element types for Slice and Map is not recommended.")])]),t._v(" "),n("p",[t._v("Here's a complete example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n Name "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner pointers")]),t._v("\n aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner values")]),t._v("\n\n aMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner pointers")]),t._v("\n aMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner values")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 creates A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 creates A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The example yields the following result upon execution:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/AGoner\nRevive main/AGoner\nRevive main/AGoner\nRevive main/BGoner\naSlice1:I am the AGoner, My name is: Injected Goner1\naSlice1:I am the AGoner, My name is: Anonymous\naSlice1:I am the AGoner, My name is: Injected Goner2\n\naSlice2:I am the AGoner, My name is: Injected Goner1\naSlice2:I am the AGoner, My name is: Anonymous\naSlice2:I am the AGoner, My name is: Injected Goner2\n\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\n\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\n")])])]),n("h3",{attrs:{id:"private-property-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#private-property-injection"}},[t._v("#")]),t._v(" Private Property Injection")]),t._v(" "),n("p",[t._v('Following the "Open-Closed Principle," attributes that modules depend on should ideally be private. Gone supports injecting into private variables of a struct.')]),t._v(" "),n("h2",{attrs:{id:"configuration-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-injection"}},[t._v("#")]),t._v(" "),n("RouterLink",{attrs:{to:"/guide/guide/config.html"}},[t._v("Configuration Injection")])],1)])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[23],{268:function(t,n){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},269:function(t,n,a){t.exports=a.p+"assets/img/image7.ca08b1e7.png"},318:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h2",{attrs:{id:"goner-and-dependency-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goner-and-dependency-injection"}},[t._v("#")]),t._v(" Goner and Dependency Injection")]),t._v(" "),n("h3",{attrs:{id:"definition-of-goner"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#definition-of-goner"}},[t._v("#")]),t._v(" Definition of Goner")]),t._v(" "),n("p",[t._v('In the Gone application, all components are required to be defined as Goners (i.e., structures that "inherit" '),n("code",[t._v("gone.Flag")]),t._v(', although Go does not have the concept of "inheritance" as seen in other languages; instead, it uses anonymous embedding). If a property of a Goner is tagged with the '),n("code",[t._v('gone:""')]),t._v(" tag, the Gone framework will attempt to automatically wire this property. Here's an example of defining a Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Injecting the above-defined AGoner into another Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n A "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The purpose of the gone tag is to inform Gone that this property needs to be automatically injected with a value")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In this example, both the injected and the injecting structures must be "),n("code",[t._v("Goner")]),t._v(" (i.e., structures embedded with "),n("code",[t._v("gone.Flag")]),t._v("). The "),n("code",[t._v('gone:"*"')]),t._v(" tag on the "),n("code",[t._v("A")]),t._v(" property of the "),n("code",[t._v("BGoner")]),t._v(" tells the framework that this property needs to be injected with a value.")]),t._v(" "),n("h3",{attrs:{id:"how-dependency-injection-is-achieved-in-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-dependency-injection-is-achieved-in-gone"}},[t._v("#")]),t._v(" How Dependency Injection is Achieved in Gone")]),t._v(" "),n("p",[t._v("In Java Spring, classes are annotated with "),n("code",[t._v("@Component")]),t._v(", "),n("code",[t._v("@Service")]),t._v(", etc., and during Spring startup, these specially annotated classes are automatically scanned, instantiated, and their annotated properties injected with corresponding values.")]),t._v(" "),n("p",[t._v("The reason Spring can achieve this functionality lies in a critical feature of Java: after Java code is compiled into a JAR file, all class bytecode is preserved, even for classes not directly dependent on the "),n("code",[t._v("main")]),t._v(" function. However, in Go, the compiled code is pruned, and only the code relevant to the "),n("code",[t._v("main")]),t._v(" function is retained in the binary. Therefore, if we merely define Goners, we'll find that our Goner code has been pruned.")]),t._v(" "),n("p",[t._v('How do we prevent our Goners from being pruned? The answer is simple: explicitly add all Goners to a "repository". In Gone, this repository is called the '),n("code",[t._v("Cemetery")]),t._v(". "),n("code",[t._v("Goner")]),t._v(' means "deceased", and '),n("code",[t._v("Cemetery")]),t._v(" is the graveyard used to bury (Bury) Goners. We can instantiate all Goners and add them to the Cemetery at program startup:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the above code, we see that "),n("code",[t._v("gone.Run")]),t._v(" can accept a function in the form of "),n("strong",[n("code",[t._v("func (cemetery gone.Cemetery) error")])]),t._v(". In practice, this function, which we call "),n("strong",[t._v("Priest")]),t._v(", is responsible for burying Goners in the cemetery.")]),t._v(" "),n("h3",{attrs:{id:"how-to-execute-business-code-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-execute-business-code-in-goners"}},[t._v("#")]),t._v(" How to Execute Business Code in Goners?")]),t._v(" "),n("p",[t._v("In Gone, we've made an interesting definition: when all properties of a Goner are injected, we say that this Goner has been resurrected (Revive). If you look at the source code of Gone, you'll find that the component responsible for managing the revived Goner status is called "),n("strong",[t._v("Heaven")]),t._v(", inspired by the mythical belief across religions that people ascend to heaven after death.")]),t._v(" "),n("p",[t._v("To execute business code in Goners, we can define a method on Goners called "),n("strong",[n("code",[t._v("AfterRevive() gone.AfterReviveError")])]),t._v(". This method is executed after a Goner is "),n("strong",[t._v("Revived")]),t._v(", and Goners possessing this method are called "),n("strong",[t._v("Prophets")]),t._v(". In practice, we typically only need to define a small number of prophets to guide code execution.")]),t._v(" "),n("p",[t._v("Here's an example; you can find the code "),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/after-revive/main.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is a function executed after revival.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Executing the above code will yield the following result:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Computer\nRevive main/Adder\nI want to compute"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("add")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),t._v(" is "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3000")]),t._v("\n")])])]),n("h3",{attrs:{id:"the-gone-command-automatically-generating-priest-functions"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#the-gone-command-automatically-generating-priest-functions"}},[t._v("#")]),t._v(" The gone Command: Automatically Generating "),n("code",[t._v("Priest")]),t._v(" Functions")]),t._v(" "),n("p",[t._v("In fact, we've already covered the core functionality of the Gone framework. However, due to limitations in Golang itself, we can't achieve the same level of convenience as Spring, where all "),n("strong",[n("code",[t._v("Goners")])]),t._v(" need to be manually "),n("strong",[t._v("buried")]),t._v(" (**"),n("code",[t._v("Bury")]),t._v("**ed) into the "),n("strong",[n("code",[t._v("Cemetery")])]),t._v(". To make Gone easier to use, we've developed a utility tool to automatically generate "),n("strong",[n("code",[t._v("Priest")])]),t._v(" functions. Below, we'll explain how to use this utility tool in a project.")]),t._v(" "),n("blockquote",[n("p",[t._v("You can find the complete code "),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/gen-code",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1)])]),t._v(" "),n("h4",{attrs:{id:"_1-install-the-utility-tool-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-install-the-utility-tool-gone"}},[t._v("#")]),t._v(" 1. Install the Utility Tool: gone")]),t._v(" "),n("p",[t._v("The utility tool, also named gone, can be installed using "),n("code",[t._v("go install")]),t._v(" as follows:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),n("p",[t._v("For more information on using the gone command, please refer to the "),n("a",{attrs:{href:"https://goner.fun/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone utility tool documentation"),n("OutboundLink")],1),t._v(".")]),t._v(" "),n("h4",{attrs:{id:"_2-create-a-new-project-named-gen-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-create-a-new-project-named-gen-code"}},[t._v("#")]),t._v(" 2. Create a New Project Named "),n("code",[t._v("gen-code")])]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" gen-code\n"),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" gen-code\ngo mod init gen-code\n")])])]),n("h4",{attrs:{id:"_3-create-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-create-goners"}},[t._v("#")]),t._v(" 3. Create Goners")]),t._v(" "),n("p",[t._v("File: goner.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is a function executed after revival.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, please note that we've added two factory functions "),n("code",[t._v("NewAdder() gone.Goner")]),t._v(" and "),n("code",[t._v("func NewComputer() gone.Goner")]),t._v(", and we've put a special comment before the functions:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n")])])]),n("p",[t._v("Please do not remove this comment, as it tells the utility tool how to generate the code.")]),t._v(" "),n("h4",{attrs:{id:"_4-use-the-utility-tool"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-use-the-utility-tool"}},[t._v("#")]),t._v(" 4. Use the Utility Tool")]),t._v(" "),n("p",[t._v("Please execute the following command in the "),n("code",[t._v("gen-code")]),t._v(" directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" main "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go\n")])])]),n("p",[t._v("This command scans the current directory and generates a "),n("strong",[t._v("priest")]),t._v(" function with the function name "),n("code",[t._v("Priest")]),t._v(", in the package named "),n("code",[t._v("main")]),t._v(", and puts the code in a file named "),n("code",[t._v("priest.go")]),t._v(".\nAfter executing the command, a file named "),n("code",[t._v("priest.go")]),t._v(" will be generated in the current directory with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h4",{attrs:{id:"_5-add-a-main-function"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-add-a-main-function"}},[t._v("#")]),t._v(" 5. Add a "),n("code",[t._v("main")]),t._v(" Function")]),t._v(" "),n("p",[t._v("File: main.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Now, we've completed the entire small Gone program. Its file structure is as follows:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v("\n├── go.mod\n├── go.sum\n├── goner.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Definition of goners")]),t._v("\n├── main.go\n└── priest.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Generated code")]),t._v("\n")])])]),n("p",[t._v("You can run the program using the command "),n("code",[t._v("go run .")]),t._v(", and the program will output the following:")]),t._v(" "),n("div",{staticClass:"language-code extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Adder\nRevive main/Computer\nI want to compute!\n1000 add 2000 is 3000\n")])])]),n("h2",{attrs:{id:"anonymous-injection-vs-named-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#anonymous-injection-vs-named-injection"}},[t._v("#")]),t._v(" Anonymous Injection vs Named Injection")]),t._v(" "),n("p",[t._v("In the preceding sections, all of our examples have demonstrated anonymous injection by type. When injecting anonymously, if there are multiple "),n("strong",[n("code",[t._v("Goners")])]),t._v(" of the same type, only one will be injected, typically the first one that was revived. Gone also supports named injection, or "),n("strong",[t._v("named burial")]),t._v(".")]),t._v(" "),n("p",[t._v("Firstly, let's take a look at the complete definition of the "),n("strong",[n("code",[t._v("Cemetery.Bury")])]),t._v(" function: "),n("strong",[n("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(". This definition serves two purposes:")]),t._v(" "),n("ol",[n("li",[t._v("It supports named burial, where the second parameter is optional, allowing you to pass a string as the "),n("strong",[t._v("ID")]),t._v(" ("),n("strong",[t._v("GonerId")]),t._v(") of the "),n("strong",[t._v("Goner")]),t._v(".")]),t._v(" "),n("li",[t._v("It supports chaining of the Bury function.")])]),t._v(" "),n("h3",{attrs:{id:"named-burial"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#named-burial"}},[t._v("#")]),t._v(" Named Burial")]),t._v(" "),n("p",[t._v("To achieve named burial, our code can be written like this:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burial of the first AGoner, ID=A1")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burial of the second AGoner, ID=A2")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Alternatively, you can decouple the construction of Goners from their burial like this:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 constructs A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 constructs A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Next is the named injection within structs, which can be better understood with an example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\ta "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous injection of an AGoner")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A1")]),t._v("\n\ta2 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A2")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Note: In the code above, the tag after the struct field:")]),t._v(" "),n("ul",[n("li",[n("code",[t._v('gone:"*"')]),t._v(" denotes anonymous injection.")]),t._v(" "),n("li",[n("code",[t._v('gone:"A1"')]),t._v(" denotes injection with the ID "),n("code",[t._v("A1")]),t._v(".")])]),t._v(" "),n("p",[t._v("In Go, whether anonymous or named, the "),n("strong",[t._v("Goner")]),t._v(" to be injected must be type-compatible; otherwise, the injection will fail.")]),t._v(" "),n("h2",{attrs:{id:"pointer-injection-value-injection-and-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-value-injection-and-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection, Value Injection, and Interface Injection")]),t._v(" "),n("p",[t._v("If the property of the injected struct is a pointer, then the injection is called "),n("strong",[t._v("pointer injection")]),t._v(". The definitions for "),n("strong",[t._v("value injection")]),t._v(" and "),n("strong",[t._v("interface injection")]),t._v(" are similar. Let's take an example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Speaker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n\ta0 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous injection of an AGoner; pointer injection")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection of A1; pointer injection")]),t._v("\n\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n\ta3 Speaker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Interface injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, "),n("code",[t._v("BGoner.a0")]),t._v(" and "),n("code",[t._v("BGoner.a1")]),t._v(" are subjected to "),n("strong",[t._v("pointer injection")]),t._v(". "),n("code",[t._v("BGoner.a2")]),t._v(" is subjected to "),n("strong",[t._v("value injection")]),t._v(", while "),n("code",[t._v("BGoner.a3")]),t._v(" is subjected to "),n("strong",[t._v("interface injection")]),t._v(".")]),t._v(" "),n("p",[n("strong",[t._v("A special note:")]),t._v(' In Go, "assigning or passing a value type involves making a copy." This means that when using '),n("strong",[t._v("value injection")]),t._v(', a new "object" is actually created, and the new and old objects are only equal at the moment of transfer. They are independent in memory. This can lead to some unexpected results. For example:')]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Value injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("In the code above, "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(" are both injected with the same Goner ("),n("code",[t._v("A1")]),t._v("). However, since it's "),n("strong",[t._v("value injection")]),t._v(", during the injection process, the framework can only copy the value of "),n("code",[t._v("A1")]),t._v(" Goner to "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(". After injection, "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(" have no connection with "),n("code",[t._v("A1")]),t._v(", and there's no connection between "),n("code",[t._v("BGoner.a1")]),t._v(" and "),n("code",[t._v("BGoner.a2")]),t._v(". There will also be three separate spaces in memory occupied by "),n("code",[t._v("AGoner")]),t._v(" type. The result of "),n("code",[t._v('fmt.Printf("a1 is eq a2: %v", g.a1 == g.a2)')]),t._v(" will be "),n("code",[t._v("false")]),t._v(".")]),t._v(" "),n("blockquote",[n("p",[t._v("Considering that "),n("strong",[t._v("Gone")]),t._v(" is primarily intended as a foundational framework, we have retained the "),n("strong",[t._v("value injection")]),t._v(" feature. However, in most cases, we recommend using "),n("strong",[t._v("pointer injection")]),t._v(" and "),n("strong",[t._v("interface injection")]),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"pointer-injection-vs-interface-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#pointer-injection-vs-interface-injection"}},[t._v("#")]),t._v(" Pointer Injection vs Interface Injection")]),t._v(" "),n("p",[t._v("During the Bury process of "),n("strong",[t._v("Goner")]),t._v(", it's required to pass a reference. This means that the first parameter of the "),n("code",[t._v("Cemetery.Bury")]),t._v(" method must be of a reference type. Both pointer injection and interface injection can pass the reference of "),n("strong",[t._v("Bury")]),t._v(" to the properties of a struct. "),n("strong",[t._v("Pointer injection")]),t._v(" is straightforward and intuitive, with a one-to-one correspondence between types, requiring little explanation.")]),t._v(" "),n("p",[t._v("Interfaces are one of the most essential designs in Go. Their purpose is to decouple the business usage from the implementation logic, allowing users of the interface to not be concerned with the implementation details. Another purpose of interfaces is to break circular dependencies. If two modules have a circular reference and they are in different packages, it violates the rule of no circular dependencies in Go, leading to compilation failure. We can use interfaces to break this circular dependency.")]),t._v(" "),n("p",[t._v("Circular dependency:\n"),n("img",{attrs:{src:a(268),alt:"Circular dependency"}})]),t._v(" "),n("p",[t._v("Using interfaces to break circular dependency:\n"),n("img",{attrs:{src:a(269),alt:"Using interfaces to break circular dependency"}})]),t._v(" "),n("p",[t._v('Using interfaces can hide the implementation details of business logic, effectively reducing the coupling between modules and better adhering to the "open-closed" principle. Therefore, we recommend using '),n("strong",[t._v("interface injection")]),t._v(". However, there are no absolutes in everything, introducing interfaces will certainly increase additional costs, so we still support "),n("strong",[t._v("pointer injection")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"slice-injection-and-map-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#slice-injection-and-map-injection"}},[t._v("#")]),t._v(" Slice Injection and Map Injection")]),t._v(" "),n("p",[t._v("Gone supports injection into "),n("code",[t._v("Slice")]),t._v(" and "),n("code",[t._v("Map")]),t._v(", meaning it supports the following syntax:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner pointers")]),t._v("\n aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner values")]),t._v("\n\n aMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner pointers")]),t._v("\n aMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner values")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[n("strong",[t._v("The rules for injection are as follows:")])]),t._v(" "),n("ul",[n("li",[n("ol",[n("li",[t._v("The element type of Slice and Map can be Goner pointer type or Goner value type, or an interface.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"2"}},[n("li",[t._v("Gone will inject all types compatible with Goner into Slice and Map.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"3"}},[n("li",[t._v("The type of Map key can only be string.")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"4"}},[n("li",[t._v("The value of Map key is the GonerId of the injected Goner. If no GonerId is specified when burying an anonymous Goner, Gone will generate one automatically.")])])])]),t._v(" "),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("WARNING")]),t._v(" "),n("p",[t._v("Using values as the element types for Slice and Map is not recommended.")])]),t._v(" "),n("p",[t._v("Here's a complete example:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Tell the framework that this struct is a Goner")]),t._v("\n Name "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner pointers")]),t._v("\n aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a slice of Goner values")]),t._v("\n\n aMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner pointers")]),t._v("\n aMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Injected property is a map of Goner values")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 creates A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 creates A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The example yields the following result upon execution:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/AGoner\nRevive main/AGoner\nRevive main/AGoner\nRevive main/BGoner\naSlice1:I am the AGoner, My name is: Injected Goner1\naSlice1:I am the AGoner, My name is: Anonymous\naSlice1:I am the AGoner, My name is: Injected Goner2\n\naSlice2:I am the AGoner, My name is: Injected Goner1\naSlice2:I am the AGoner, My name is: Anonymous\naSlice2:I am the AGoner, My name is: Injected Goner2\n\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\n\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\n")])])]),n("h3",{attrs:{id:"private-property-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#private-property-injection"}},[t._v("#")]),t._v(" Private Property Injection")]),t._v(" "),n("p",[t._v('Following the "Open-Closed Principle," attributes that modules depend on should ideally be private. Gone supports injecting into private variables of a struct.')]),t._v(" "),n("h2",{attrs:{id:"configuration-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-injection"}},[t._v("#")]),t._v(" "),n("RouterLink",{attrs:{to:"/guide/guide/config.html"}},[t._v("Configuration Injection")])],1)])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/24.edebe9f3.js b/assets/js/24.bc0ab487.js similarity index 99% rename from assets/js/24.edebe9f3.js rename to assets/js/24.bc0ab487.js index fe1a3a3..475a92c 100644 --- a/assets/js/24.edebe9f3.js +++ b/assets/js/24.bc0ab487.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[24],{279:function(t,s,n){t.exports=n.p+"assets/img/image9.48d32078.png"},280:function(t,s,n){t.exports=n.p+"assets/img/image10.91a30027.png"},336:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-mysql"}},[t._v("#")]),t._v(" Web + MySQL")]),t._v(" "),s("p",[t._v("In this document, I will demonstrate how to create a production-ready web project and provide a brief introduction to the project. The project uses MySQL as the database and Docker Compose to manage containers.")]),t._v(" "),s("h2",{attrs:{id:"install-the-gone-tool"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#install-the-gone-tool"}},[t._v("#")]),t._v(" Install the gone Tool")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("For more information about the gone command, refer to: "),s("a",{attrs:{href:"https://goner.fun/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone Tool"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"create-a-project-using-the-gone-command"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#create-a-project-using-the-gone-command"}},[t._v("#")]),t._v(" Create a Project Using the gone Command")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone create "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-t")]),t._v(" web+mysql web-mysql-docker\n")])])]),s("p",[t._v("The above command will create a directory named web-mysql-docker in the current directory.")]),t._v(" "),s("h2",{attrs:{id:"compile-and-run"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#compile-and-run"}},[t._v("#")]),t._v(" Compile And Run")]),t._v(" "),s("blockquote",[s("p",[t._v("Ensure you have make installed; if not, please install it first.\nEnsure you have docker and docker compose installed; if not, refer to: https://docs.docker.com/engine/install/.")])]),t._v(" "),s("p",[t._v("Execute the following commands:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Enter the project directory")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-mysql-docker\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Generate the Priest functions: https://goner.fun/guide/auto-gen-priest.html")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Start the MySQL service")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker")]),t._v(" compose up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v(" mysql\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Compile and run")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("p",[t._v("The logs should display as follows:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ web-mysql-docker "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" install-gone\ngo "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\ngo mod tidy\ngo generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\ngo run cmd/server/main.go\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/heaven\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/cemetery\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/tracer/tracer\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("local")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/default.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/local.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/xorm/engine\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/cmux/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/proxy\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/router\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/sysProcessor\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/responser\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/httpInjector\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/controller/demoController\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/AuthorizeMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/PubMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/db\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/demoService\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/authRouter\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/pubRouter\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" Running "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"debug"')]),t._v(" mode. Switch to "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"release"')]),t._v(" mode "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" production.\n - using env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("export")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token assign-left variable"}},[t._v("GIN_MODE")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v("release\n - using code: gin.SetMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gin.ReleaseMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/error --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/echo --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-query --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject/:key --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/inject-http-body --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-http-struct --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/page --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" PUT /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" DELETE /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/goner/gin/server.go:46"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Server Listen At :8080\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go:90"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/cemetery.go:329"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("Anonymous Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/works/gone-io/web-app/demo/web-mysql-docker/cmd/server/main.go:28"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("before start\n")])])]),s("h2",{attrs:{id:"api-testing"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#api-testing"}},[t._v("#")]),t._v(" API Testing")]),t._v(" "),s("p",[t._v("I'm going to test using the built-in HTTP Request plugin in Goland. Open the project in Goland, navigate to the file "),s("code",[t._v("tests/api/user.http")]),t._v(", and select the "),s("code",[t._v("dev")]),t._v(" environment to run, as shown below:")]),t._v(" "),s("p",[s("img",{attrs:{src:n(279),alt:"Run"}})]),t._v(" "),s("p",[t._v("Execution Result:\n"),s("img",{attrs:{src:n(280),alt:"Result"}})]),t._v(" "),s("h2",{attrs:{id:"project-brief-introduction"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#project-brief-introduction"}},[t._v("#")]),t._v(" Project Brief Introduction")]),t._v(" "),s("h3",{attrs:{id:"directory-structure"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#directory-structure"}},[t._v("#")]),t._v(" Directory Structure")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# File containing the main method of the project")]),t._v("\n├── config "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Project configuration directory")]),t._v("\n│ ├── default.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Default configuration")]),t._v("\n│ ├── dev.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Development environment configuration")]),t._v("\n│ ├── local.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Local environment configuration")]),t._v("\n│ └── prod.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Production environment configuration")]),t._v("\n├── docker-compose.yaml "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# docker-compose, defines mysql and web containers")]),t._v("\n├── go.mod\n├── internal\n│ ├── controller "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Controller")]),t._v("\n│ │ └── demo_ctr.go\n│ ├── interface "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Interface directory")]),t._v("\n│ │ ├── domain "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Domain models")]),t._v("\n│ │ │ ├── demo.go\n│ │ │ ├── page.go\n│ │ │ └── user.go\n│ │ ├── entity "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Entity models")]),t._v("\n│ │ │ └── User.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Service interfaces")]),t._v("\n│ │ └── i_demo.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Typically one interface per file, file name starts with `i_`, interface name starts with `I`, e.g., `IDemo`")]),t._v("\n│ ├── master.go\n│ ├── middleware "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Middleware directory, can define unified processing logic such as authentication")]),t._v("\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Module directory")]),t._v("\n│ │ └── demo "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Demo module")]),t._v("\n│ │ ├── db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Implementation of demo module's database interface")]),t._v("\n│ │ ├── demo_svc.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Demo Service, implementing `service.IDemo` interface")]),t._v("\n│ │ ├── error.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Error code definition for the current directory")]),t._v("\n│ │ └── i_db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Database interface of the demo module")]),t._v("\n│ ├── pkg "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Public utilities directory")]),t._v("\n│ │ └── utils\n│ │ └── error.go\n│ ├── priest.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Not present when created by gone create, generated by running `make gone`")]),t._v("\n│ └── router "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Router directory")]),t._v("\n│ ├── auth_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Defines authenticated router groups")]),t._v("\n│ └── pub_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Defines unauthenticated router groups")]),t._v("\n├── scripts "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Script directory, used to store some scripts")]),t._v("\n│ └── mysql\n│ └── initdb.d "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# mysql initdb.d directory, all sql files under this directory will be executed when mysql defined in docker-compose starts for the first time")]),t._v("\n│ └── user.sql\n└── tests "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Integration test directory")]),t._v("\n └── api "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# API test directory")]),t._v("\n ├── demo.http\n ├── http-client.env.json\n └── user.http\n")])])]),s("h3",{attrs:{id:"project-features"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#project-features"}},[t._v("#")]),t._v(" Project Features")]),t._v(" "),s("ul",[s("li",[t._v("Ready to use, no additional configuration needed to run")]),t._v(" "),s("li",[t._v("Provides a "),s("code",[t._v("Dockerfile")]),t._v(" for easy deployment to various container environments")]),t._v(" "),s("li",[t._v("Integrated commands like gone, go, docker, and docker compose using Makefile")]),t._v(" "),s("li",[t._v("Automatically generates Priest functions for smoother development")]),t._v(" "),s("li",[t._v("Utilizes the gone framework, incorporating dependency injection")]),t._v(" "),s("li",[t._v("Decoupling through interface usage")]),t._v(" "),s("li",[t._v("Supports configuration writing for different environments")]),t._v(" "),s("li",[t._v("Integrated MySQL database")])]),t._v(" "),s("h3",{attrs:{id:"partial-code-explanation"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#partial-code-explanation"}},[t._v("#")]),t._v(" Partial Code Explanation")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// CRUD operations for user data under the demo module, mounted on authRouter for demonstration purposes")]),t._v("\n ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/users"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Body injection, parses HTTP body into a struct based on contentType, supports json, xml, form-data, x-www-form-urlencoded, etc.")]),t._v("\n req "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ListUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/page"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n query domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PageQuery "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Query injection")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PageUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("query"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PUT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n req "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Body injection")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UpdateUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DELETE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DeleteUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("The above code snippet is taken from the "),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v(" file.")]),t._v(" "),s("h3",{attrs:{id:"http-injection"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-injection"}},[t._v("#")]),t._v(" HTTP Injection")]),t._v(" "),s("p",[t._v("As observed, "),s("strong",[t._v("Gone")]),t._v(" supports dependency injection on route handling functions. This is achieved by using an anonymous struct as the input parameter of the handling function and marking the struct attributes with special tags.\nThe injection tags used in the above code are:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v('gone:"http,body"')]),t._v(": Body injection, parses HTTP body into a struct based on contentType, supports json, xml, form-data, x-www-form-urlencoded, etc. The injected struct attribute should be of type "),s("strong",[t._v("struct")]),t._v(" or "),s("strong",[t._v("struct pointer")]),t._v(".")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,query"')]),t._v(": Query injection, the injected struct attribute can be of type "),s("strong",[t._v("struct")]),t._v(", "),s("strong",[t._v("struct pointer")]),t._v(", "),s("strong",[t._v("string")]),t._v(", "),s("strong",[t._v("Number (int, uint, float64, etc.)")]),t._v(", or "),s("strong",[t._v("Slice of string or Number")]),t._v(".")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,param"')]),t._v(": URL parameter injection, injected from the route defined as "),s("code",[t._v(":id")]),t._v(". The injected struct attribute should be of type "),s("strong",[t._v("string")]),t._v(" or "),s("strong",[t._v("Number")]),t._v(".")])]),t._v(" "),s("p",[t._v("Additionally, handling functions also support injection of some special structs or pointers (pointers are recommended):")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("gone.Content")]),t._v(": Request context")]),t._v(" "),s("li",[s("code",[t._v("http.Request")]),t._v(": HTTP request")]),t._v(" "),s("li",[s("code",[t._v("http.Header")]),t._v(": Request header")]),t._v(" "),s("li",[s("code",[t._v("url.Url")]),t._v(": URL")])]),t._v(" "),s("p",[t._v("The framework supports more injection tags and types, please refer to the documentation "),s("a",{attrs:{href:"https://goner.fun/en/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP Injection"),s("OutboundLink")],1),t._v(".")]),t._v(" "),s("h3",{attrs:{id:"request-handling-function-parameters"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#request-handling-function-parameters"}},[t._v("#")]),t._v(" Request Handling Function Parameters")]),t._v(" "),s("p",[t._v("Request handling functions can have the following return formats:")]),t._v(" "),s("ol",[s("li",[t._v("No return parameters")]),t._v(" "),s("li",[t._v("Return a non-"),s("code",[t._v("error")]),t._v(" parameter")]),t._v(" "),s("li",[t._v("Return data and "),s("code",[t._v("error")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[24],{279:function(t,s,n){t.exports=n.p+"assets/img/image9.48d32078.png"},280:function(t,s,n){t.exports=n.p+"assets/img/image10.91a30027.png"},327:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-mysql"}},[t._v("#")]),t._v(" Web + MySQL")]),t._v(" "),s("p",[t._v("In this document, I will demonstrate how to create a production-ready web project and provide a brief introduction to the project. The project uses MySQL as the database and Docker Compose to manage containers.")]),t._v(" "),s("h2",{attrs:{id:"install-the-gone-tool"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#install-the-gone-tool"}},[t._v("#")]),t._v(" Install the gone Tool")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("For more information about the gone command, refer to: "),s("a",{attrs:{href:"https://goner.fun/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone Tool"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"create-a-project-using-the-gone-command"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#create-a-project-using-the-gone-command"}},[t._v("#")]),t._v(" Create a Project Using the gone Command")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone create "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-t")]),t._v(" web+mysql web-mysql-docker\n")])])]),s("p",[t._v("The above command will create a directory named web-mysql-docker in the current directory.")]),t._v(" "),s("h2",{attrs:{id:"compile-and-run"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#compile-and-run"}},[t._v("#")]),t._v(" Compile And Run")]),t._v(" "),s("blockquote",[s("p",[t._v("Ensure you have make installed; if not, please install it first.\nEnsure you have docker and docker compose installed; if not, refer to: https://docs.docker.com/engine/install/.")])]),t._v(" "),s("p",[t._v("Execute the following commands:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Enter the project directory")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-mysql-docker\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Generate the Priest functions: https://goner.fun/guide/auto-gen-priest.html")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Start the MySQL service")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker")]),t._v(" compose up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v(" mysql\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Compile and run")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("p",[t._v("The logs should display as follows:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ web-mysql-docker "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" install-gone\ngo "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\ngo mod tidy\ngo generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\ngo run cmd/server/main.go\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/heaven\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/cemetery\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/tracer/tracer\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("local")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/default.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/local.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/xorm/engine\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/cmux/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/proxy\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/router\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/sysProcessor\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/responser\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/httpInjector\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/controller/demoController\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/AuthorizeMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/PubMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/db\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/demoService\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/authRouter\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/pubRouter\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" Running "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"debug"')]),t._v(" mode. Switch to "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"release"')]),t._v(" mode "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" production.\n - using env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("export")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token assign-left variable"}},[t._v("GIN_MODE")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v("release\n - using code: gin.SetMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gin.ReleaseMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/error --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/echo --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-query --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject/:key --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/inject-http-body --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-http-struct --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/page --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" PUT /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" DELETE /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/goner/gin/server.go:46"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Server Listen At :8080\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go:90"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/cemetery.go:329"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("Anonymous Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/works/gone-io/web-app/demo/web-mysql-docker/cmd/server/main.go:28"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("before start\n")])])]),s("h2",{attrs:{id:"api-testing"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#api-testing"}},[t._v("#")]),t._v(" API Testing")]),t._v(" "),s("p",[t._v("I'm going to test using the built-in HTTP Request plugin in Goland. Open the project in Goland, navigate to the file "),s("code",[t._v("tests/api/user.http")]),t._v(", and select the "),s("code",[t._v("dev")]),t._v(" environment to run, as shown below:")]),t._v(" "),s("p",[s("img",{attrs:{src:n(279),alt:"Run"}})]),t._v(" "),s("p",[t._v("Execution Result:\n"),s("img",{attrs:{src:n(280),alt:"Result"}})]),t._v(" "),s("h2",{attrs:{id:"project-brief-introduction"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#project-brief-introduction"}},[t._v("#")]),t._v(" Project Brief Introduction")]),t._v(" "),s("h3",{attrs:{id:"directory-structure"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#directory-structure"}},[t._v("#")]),t._v(" Directory Structure")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# File containing the main method of the project")]),t._v("\n├── config "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Project configuration directory")]),t._v("\n│ ├── default.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Default configuration")]),t._v("\n│ ├── dev.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Development environment configuration")]),t._v("\n│ ├── local.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Local environment configuration")]),t._v("\n│ └── prod.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Production environment configuration")]),t._v("\n├── docker-compose.yaml "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# docker-compose, defines mysql and web containers")]),t._v("\n├── go.mod\n├── internal\n│ ├── controller "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Controller")]),t._v("\n│ │ └── demo_ctr.go\n│ ├── interface "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Interface directory")]),t._v("\n│ │ ├── domain "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Domain models")]),t._v("\n│ │ │ ├── demo.go\n│ │ │ ├── page.go\n│ │ │ └── user.go\n│ │ ├── entity "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Entity models")]),t._v("\n│ │ │ └── User.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Service interfaces")]),t._v("\n│ │ └── i_demo.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Typically one interface per file, file name starts with `i_`, interface name starts with `I`, e.g., `IDemo`")]),t._v("\n│ ├── master.go\n│ ├── middleware "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Middleware directory, can define unified processing logic such as authentication")]),t._v("\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Module directory")]),t._v("\n│ │ └── demo "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Demo module")]),t._v("\n│ │ ├── db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Implementation of demo module's database interface")]),t._v("\n│ │ ├── demo_svc.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Demo Service, implementing `service.IDemo` interface")]),t._v("\n│ │ ├── error.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Error code definition for the current directory")]),t._v("\n│ │ └── i_db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Database interface of the demo module")]),t._v("\n│ ├── pkg "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Public utilities directory")]),t._v("\n│ │ └── utils\n│ │ └── error.go\n│ ├── priest.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Not present when created by gone create, generated by running `make gone`")]),t._v("\n│ └── router "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Router directory")]),t._v("\n│ ├── auth_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Defines authenticated router groups")]),t._v("\n│ └── pub_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Defines unauthenticated router groups")]),t._v("\n├── scripts "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Script directory, used to store some scripts")]),t._v("\n│ └── mysql\n│ └── initdb.d "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# mysql initdb.d directory, all sql files under this directory will be executed when mysql defined in docker-compose starts for the first time")]),t._v("\n│ └── user.sql\n└── tests "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Integration test directory")]),t._v("\n └── api "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# API test directory")]),t._v("\n ├── demo.http\n ├── http-client.env.json\n └── user.http\n")])])]),s("h3",{attrs:{id:"project-features"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#project-features"}},[t._v("#")]),t._v(" Project Features")]),t._v(" "),s("ul",[s("li",[t._v("Ready to use, no additional configuration needed to run")]),t._v(" "),s("li",[t._v("Provides a "),s("code",[t._v("Dockerfile")]),t._v(" for easy deployment to various container environments")]),t._v(" "),s("li",[t._v("Integrated commands like gone, go, docker, and docker compose using Makefile")]),t._v(" "),s("li",[t._v("Automatically generates Priest functions for smoother development")]),t._v(" "),s("li",[t._v("Utilizes the gone framework, incorporating dependency injection")]),t._v(" "),s("li",[t._v("Decoupling through interface usage")]),t._v(" "),s("li",[t._v("Supports configuration writing for different environments")]),t._v(" "),s("li",[t._v("Integrated MySQL database")])]),t._v(" "),s("h3",{attrs:{id:"partial-code-explanation"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#partial-code-explanation"}},[t._v("#")]),t._v(" Partial Code Explanation")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// CRUD operations for user data under the demo module, mounted on authRouter for demonstration purposes")]),t._v("\n ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/users"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Body injection, parses HTTP body into a struct based on contentType, supports json, xml, form-data, x-www-form-urlencoded, etc.")]),t._v("\n req "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ListUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/page"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n query domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PageQuery "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Query injection")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PageUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("query"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PUT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n req "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Body injection")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UpdateUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DELETE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Url parameter injection, injected from the route defined as `:id`")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DeleteUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("The above code snippet is taken from the "),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v(" file.")]),t._v(" "),s("h3",{attrs:{id:"http-injection"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-injection"}},[t._v("#")]),t._v(" HTTP Injection")]),t._v(" "),s("p",[t._v("As observed, "),s("strong",[t._v("Gone")]),t._v(" supports dependency injection on route handling functions. This is achieved by using an anonymous struct as the input parameter of the handling function and marking the struct attributes with special tags.\nThe injection tags used in the above code are:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v('gone:"http,body"')]),t._v(": Body injection, parses HTTP body into a struct based on contentType, supports json, xml, form-data, x-www-form-urlencoded, etc. The injected struct attribute should be of type "),s("strong",[t._v("struct")]),t._v(" or "),s("strong",[t._v("struct pointer")]),t._v(".")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,query"')]),t._v(": Query injection, the injected struct attribute can be of type "),s("strong",[t._v("struct")]),t._v(", "),s("strong",[t._v("struct pointer")]),t._v(", "),s("strong",[t._v("string")]),t._v(", "),s("strong",[t._v("Number (int, uint, float64, etc.)")]),t._v(", or "),s("strong",[t._v("Slice of string or Number")]),t._v(".")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,param"')]),t._v(": URL parameter injection, injected from the route defined as "),s("code",[t._v(":id")]),t._v(". The injected struct attribute should be of type "),s("strong",[t._v("string")]),t._v(" or "),s("strong",[t._v("Number")]),t._v(".")])]),t._v(" "),s("p",[t._v("Additionally, handling functions also support injection of some special structs or pointers (pointers are recommended):")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("gone.Content")]),t._v(": Request context")]),t._v(" "),s("li",[s("code",[t._v("http.Request")]),t._v(": HTTP request")]),t._v(" "),s("li",[s("code",[t._v("http.Header")]),t._v(": Request header")]),t._v(" "),s("li",[s("code",[t._v("url.Url")]),t._v(": URL")])]),t._v(" "),s("p",[t._v("The framework supports more injection tags and types, please refer to the documentation "),s("a",{attrs:{href:"https://goner.fun/en/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP Injection"),s("OutboundLink")],1),t._v(".")]),t._v(" "),s("h3",{attrs:{id:"request-handling-function-parameters"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#request-handling-function-parameters"}},[t._v("#")]),t._v(" Request Handling Function Parameters")]),t._v(" "),s("p",[t._v("Request handling functions can have the following return formats:")]),t._v(" "),s("ol",[s("li",[t._v("No return parameters")]),t._v(" "),s("li",[t._v("Return a non-"),s("code",[t._v("error")]),t._v(" parameter")]),t._v(" "),s("li",[t._v("Return data and "),s("code",[t._v("error")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/25.8259168e.js b/assets/js/25.8259168e.js deleted file mode 100644 index 14324f8..0000000 --- a/assets/js/25.8259168e.js +++ /dev/null @@ -1 +0,0 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[25],{256:function(t,n){t.exports="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjQAAAEECAYAAAA/Ga4zAAAACXBIWXMAAAsTAAALEwEAmpwYAAATvElEQVR4nO3df2xV9f3H8dfBhrnGGoxyGzI7wWsaQEGjoriB3NsoIERBScC4gEaUZR31By5kEKX3opFA5owTpoKIWAUEf26ZEwRaNgzLUJdmVsFCW7ZC2sqAjXUtUPh8/yD3fCk/SpH2nvs+9/lIbtJy7z3n3Hs/HJ587qe3nnPOCQAAwLAeQR8AAADA+SJoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeYEFTTwel+d5GXuJx+NBPTUAQohzHtC9POecC2THnqeAdt0pmX58AGzJ9HNKph8fcDa85QQAAMwjaAAAgHkEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMwjaAAAgHkEDQAAMI+gAQAA5uUEteO+ffvK87ygdt8pmX58OD+xWEzl5eVBHwayBOc8BC3s5zx+2zayFmMQ6cR4Q9DCPgZ5ywkAAJhH0AAAAPMIGgAAYF4ogyaZTMrzPP3tb38L+lAAoNsFdc779ttv5XmeJk2alNb9AqcTyqA5fPiwJIV68RMApAR1zkvt7+jRo2ndL3A6gQfN5s2bNWzYMC1YsED33HOPotGoZs2a5f8F3bhxo4YNG6b8/HyNGTNGZWVl7e576623Kj8/Xw888ID+9Kc/nbL9f/7znxozZozGjBmj+vr6Drf33nvv6dprr1VBQYFeeeUVDRs2TM8995wkqbKyUiNHjtTFF1+seDyuzZs3n/bxdLR9AAjbOU+S9u/frylTpqigoEATJ05UQ0NDVz1dQOe5gKR2/eGHHzpJTpIrLCz0v3722Wdda2ury8vLc5LcxIkT/a+rq6vdP/7xD/+2Q4cO9b9uampys2fPdpJcRUWFu/76650k98Ybb3S4vcbGRn8bAwcO9L8uLi52+/fv9297++23+9dVV1e3e0wdbR+ZJ8DhjywUxnPeiduIRCIuEok4Se7+++8P4BnG2YT9nBf4DE3KhAkTtH37dm3atEmS9Oabb2rfvn2aOXOmPvjgA82ZM0fXXXedJGnXrl1atWqVJOlXv/qVtmzZot/85jcaNWqUdu3a5W/zwQcf1BdffKFEIqHJkyd3uL2NGzdKkp5//nlVVVVp3rx5/nb+8Ic/6ODBgyopKdHrr7+up59+WpL0zjvvtHsMHW0fAE4UhnNeSiQSUW1trWpqahSJRPTee++pra2ti58xoGOBfVLwyWKxmCRp+PDhko5Pm0YiETU2Nuqpp5465fZ1dXWSpOuvv16SVFJSopKSEknS+++/L0mqqalpd5+Otpeaur3lllvaHY/0/0Hy4osv6sUXXzzlzzuzfQA4URjOeSk//vGPlZubK0m6+eab9fvf/17V1dUaMGDAGR490PUyZoZmy5YtkqQ9e/ZIkvr06aMPP/xQCxcu1J133qnq6moVFxf7tx84cKCk47MikrRu3TrNnTtXO3bs8G8zefJkSVIikdCuXbs63N6gQYMkSdXV1ZKkqqoq/7rLLrtMkjR9+nR99dVXqqys1KeffqqZM2e2ewwdbR8AThSGc15KZWWl/3VqO9Fo9JyfE+B8ZMwMzYoVKzRgwAD97ne/kySNGjXKXznfp08fff311/5iNuecRo4cKUmaM2eOGhoaNHfuXDU1Nemxxx7zt/nYY49pwIABmj17tmbPnq3x48efcXujR4+WJBUXF2v9+vVavny5v50f/ehHkqTVq1frhhtu0IoVK/TJJ5/o3XffVb9+/fzbdXS8AHCiMJzzUmpqajRp0iTl5uaqpqZG48aNU8+ePbv0+QLOKqjFOzppgVxqIZskN2TIENfQ0OCam5vd4MGD/T8fO3ask+Tmz5/vnHNu+vTp/nWS3MqVK51zzl8g9/nnn7v//ve//kK18vLyDrc3f/58l5eX5yKRiJswYYKT5EpKSpxzzr355pvt7jt9+nTX1tbW7jGd7XiRWQIc/shCYTznNTU1+YubU/uMRCJu69at6XpacQ7Cfs7LmKBZuHCha2lpcfX19afctq6uzh0+fPi029m3b5+rqqpyra2tnd736bZXWVnpXnrpJbdlyxbX1tbmFixY4CS5V155pd3tdu7c6Q4cOHDO20fmCftfbmSWMJ/znHPu6NGjrr6+/pToQeYI+zkvY95ykqQLL7xQP/jBD0758yuuuOKM97nkkkt0ySWXnNN+Tre9/Px8zZw5UwcPHlQkElFTU5Py8vJUVFTU7nZXXnnld9o+AJwsLOc8SerRo8dpHwuQLp5zwSzwSP0a8+3bt+vdd9/V7bffriFDhgRxKL6amhpt2rRJ27ZtU9++fTV27Fj98Ic/DPSY0H1SYxBIB855CFrYz3mBBw0QFMYg0onxhqCFfQxmzI9tAwAAfFcEDQAAMI+gAQAA5hE0AADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzLqF99gM5JJBJKJpNBH0YoeJ4X9CEgizDeulaYPyQO544ZGgAAYB4zNIaVlpYqkUgEfRhmhf1jwJFZGG9dh5kunA4zNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gyXEVFRdCHAABAxiNoDPA876w/nl1RUaF4PM6PcQMAshJBk+FisZhisZiSyeRpwyYVMvF4XCNGjCBoAABZyXMBfdITHzLVealoOVnfvn1VV1fnf88H7Z0bxiDSifHWdVIfrMfzeW7CPgYJGiPi8XiH62mImXPHGEQ6Md66DkHz3YR9DPKWkxGlpaVBHwIAABmLoDEitZbmdJidAQBkO345pSGlpaX8GDeA08rGX9iYbY85zG8XdQXW0Bhz8loaZme+O8Yg0qm7xxvjOdy64vUN+xjhLSdjWEsDAMCpCBpjTlxLw+wMAADHETQGMUsDAEB7rKExqqKi4ow/9YTOYQwinVhDg/PBGpqzC80MTepXA2TLJR6PB34M6b4kk8mghxkAIEOFJmgkKZFIyDnHJYQX1goBADoSqqABAADZiaABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNACBjHDt2THV1ddq7d2/QhwJjCBoAQLe7+OKL5XmefykoKNDcuXN17NgxSZJzTkuXLlWvXr3Ur18/9e7dW9FoVG+//XbARw4rCBoAQFqUlZVp//79amho0OzZs1VaWqotW7ZIkt566y099NBDKisr05EjR7R3715NmzZN9957rz777LOAjxwWEDQAgLS46KKL1KtXL+Xn5+vuu++WJDU3N0uSnnjiCc2fP1/jxo1TTk6OLr30Us2cOVNPPfWUdu/eLUk6evSonnnmGRUUFCg/P18zZsxQS0uLWltbddNNN2np0qW6+uqrFY1G9eqrr/r7/etf/6pbb71V+fn5mjJliv71r39JkpYtW6Y5c+Zo6tSp+ulPf5rmZwNdjaABAKTFunXrtGzZMi1atEg/+clPVFhYqBEjRqixsVFNTU267bbb/NsePnxYR44c0ZNPPqmxY8dKkl577TUtWLBAv/zlL7VmzRqtWbNGzz77rI4dO6atW7dqwYIFev755xWPx/Xwww+rtbVVjY2Nuvnmm3XddddpzZo12rdvnx9Te/bs0dNPP63du3fr3nvvDeQ5QdfJCfoAAADZ4dNPP1VdXZ3a2tr05ZdfqmfPnqqtrVVLS4uk4+tsJGnnzp266qqr/PsVFRVpw4YNWrJkiR599FH9/Oc/lySVlpYqmUxq1qxZkqSXX35Z8Xhct9xyi5YuXaq6ujp9/PHHuvzyy/XCCy/I8zzl5+erf//+amhokCRFIhF99NFH6tGD/99bxyuYgVjlDyCMksmkPvroI61bt067d+/WoEGDtHjxYvXp00eS/LUyV1xxhXbs2KEdO3aouLjYv/+2bdt04403+t/369dP9fX1/vcFBQWSpLy8PEnSoUOHtHPnTtXX16tHjx7yPE/9+/eXJP/8ettttxEzIcGrGIBf/OIX8jzvlIVurPIHkC1ycnI0aNAgVVZWKj8/X3l5eSovL/evi0ajikaj2rZtm3+fG264oV3AfPPNNxo1apT//QUXXHDKfnr16qUhQ4aoublZzc3NOnDggDZv3qzCwsJufHQIAkGTZkeOHNHixYuVl5en1atXt7uOVf4Awqy+vl7bt29XVVWV5s2bp5deekl33XWXPM/TvHnztHjxYq1Zs0YtLS2qqqrS448/ro0bN/r3Hz9+vJYvX66amhodPnxYK1eu1OjRozvc5/Dhw7V161Z99tln8jxPL7zwgu655x5mZUKIVzTN1q9fr+9///v69a9/reXLl6utrc2/jlX+AMKspKRE/fv31zXXXKPf/va3Ki4u1s9+9jNJ0rRp0zRr1ixNnDhRubm5uuaaa/SXv/xFZWVl/szLpEmTdOjQIUWjUX3ve99Ta2ur7rvvvjPuz/M8jRw5UjNmzNCIESOUm5urRYsWaeXKlcrJyZHneYRNmLiAdPWuE4mESyQSXbrN7nDfffe5J554wu3du9dJchs2bHDOOdfQ0OAkuc8//9y/7aFDh/zLkSNHnHPOLV682OXl5bmFCxe6TZs2ucsvv9w9+eSTrrm52UlyhYWFbu3atW7q1KlOkmtpafG3XVJS4jZt2uTGjh3rhg8f7pxz7plnnnGS3KhRo9zGjRvT/4R0Une8vgEOf2Sh7h5vYRnPzc3N7ssvv3T//ve/T3t9W1ubq66udrt27XLHjh3r9Habmprc3//+d9fS0tJVh5pWXfH6hmWMnAk/5ZRGBw4c0IoVK7R161ZdeumluuOOO7Rq1SoVFRVpz549kljlDyC75ebm6uqrrz7j9RdccEG7c2Nn9e7dW7179z6fQ0OG41+wNHr//fclHZ82jUaj+uMf/6hVq1appaWFVf4AAJwH/hVLo2XLlmnq1KlasmSJlixZorVr1+rgwYNau3Ytq/wBADgPBE2a1NbW6s9//rMeeeQRFRUVqaioSCNHjtS4ceO0YsUKVvkDAHAeWEOTJqtXr1ZhYaEGDx7c7s8nTJigKVOm6D//+Y+mTZum3bt3a+LEif71Q4cOVVlZmd544w1Jx9+ueu211xSNRiVJQ4YMOadV/tLxNTOs8gcAhInnnHOB7Njz1JW7TiaTko4vkrXuf//7n2pra1VQUOAvEj7R0aNHVVtbq549e6qgoECe53Vqu99++60aGxt11VVX6cILL+zqw+5W3fH6dvUYBDrS3eON8RxuXfH6hn2MMEOTgVjlDwDAueG9BgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzMsJ+gAAAF3D87ygDwEIDEEDACHgnAv6ENImHo9rxIgRSiQSQR8KMghBAwAwo6Kiwr9IImrgYw0NAMCMZDIZ9CEgQxE0AAATTpyZkY7HDTM0SCFoAAAmMDuDjoQqaBKJhDzP4xLCC/8LA7LbybMzKczSICU0QVNaWirnXFZcSktLs+4xn/i4AWQfZmdwNqEJGgBAOJ1pdiaFWRpIBA0AIMMxO4POIGgAABnrxNmZ1NvsKeXl5YrFYpKYpQFBAwDIYMlk0g+Zk4MlFoupvLy8Xdgge/FJwQCAjFVeXn7W28RiMcViMX82h7jJTgQNACAUCJnsxltOAADAPIIGAACYR9AAAADzCBoAAGAeQQMAAMzjp5wMSyaTfILmefI8L+hDQBZhvAHdhxkaAABgnudO/BzpdO7Y8xTQrgFJjEGkF+MNQQv7GGSGBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOYRNAAAwDyCBgAAmEfQAAAA8wgaAABgHkEDAADMI2gAAIB5BA0AADCPoAEAAOblBLXjWCwmz/OC2j2gWCwW9CEgi3DOQ9DCfs7znHMu6IMAAAA4H7zlBAAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPMIGgAAYB5BAwAAzCNoAACAeQQNAAAwj6ABAADmETQAAMA8ggYAAJhH0AAAAPP+D7qGTX8Q8og4AAAAAElFTkSuQmCC"},257:function(t,n,s){t.exports=s.p+"assets/img/image7.ca08b1e7.png"},347:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h2",{attrs:{id:"goner和依赖注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goner和依赖注入"}},[t._v("#")]),t._v(" Goner和依赖注入")]),t._v(" "),n("h3",{attrs:{id:"goner的定义"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goner的定义"}},[t._v("#")]),t._v(" Goner的定义")]),t._v(" "),n("p",[t._v("在Gone应用中,所有的组件都被要求定义为Goner(就是“继承”了"),n("code",[t._v("gone.Flag")]),t._v("的结构体,实际上golang中根本没有“集成”这个概念,它有的只有匿名嵌套);如果Goner的某个属性标注了"),n("code",[t._v('gone:""')]),t._v("标签,Gone框架将尝试自动装配该属性。下面是定义一个Goner的例子:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("在另一个Goner中注入上面定义的AGoner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n A "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//gone标签的作用在于告诉Gone,该属性需要被自动注入一个值")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("其中,注入的和被注入的结构体都要求是"),n("code",[t._v("Goner")]),t._v("(也就是匿名嵌套了"),n("code",[t._v("gone.Flag")]),t._v("的结构体),"),n("code",[t._v("BGoner")]),t._v("的"),n("code",[t._v("A")]),t._v("属性的"),n("code",[t._v('gone:"*"')]),t._v("标签的作用在于告诉框架:这个属性需要被注入一个值。")]),t._v(" "),n("h3",{attrs:{id:"在gone中是如何完成依赖注入的"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#在gone中是如何完成依赖注入的"}},[t._v("#")]),t._v(" 在Gone中是如何完成依赖注入的?")]),t._v(" "),n("p",[t._v("在Java Spring中,给class打上"),n("code",[t._v("@Component")]),t._v("、"),n("code",[t._v("@Service")]),t._v("等标注,Spring启动时会自动扫描到这些特殊的类,然后实例化他们并且给他们有特定标注的属性注入对应的值。")]),t._v(" "),n("p",[t._v("Spring之所以能够实现这样的功能,Java有一个特性很关键,就是Java代码在编译成jar后,会保留所有class的字节码,哪怕是没有被"),n("code",[t._v("main")]),t._v("函数依赖的class代码;然而,在Golang中,编译后的代码会被裁剪,二进制文件中只会保留"),n("code",[t._v("main")]),t._v("函数依赖的相关代码。所以我们仅是定义Goner,在编译后我们会发现我们Goner代码全部被裁剪了。")]),t._v(" "),n("p",[t._v("如何让我们的"),n("strong",[t._v("Goners")]),t._v("不被裁剪掉呢?答案很简单,我们显式的将所有Goner加入到一个”仓库“中;在Gone中,这个仓库叫做"),n("code",[t._v("Cemetery")]),t._v("。"),n("code",[t._v("Goner")]),t._v("有“死者”的意思;"),n("code",[t._v("Cemetery")]),t._v("是墓地,用于埋葬(Bury)"),n("code",[t._v("Goner")]),t._v("。我们可以在程序启动时,将所有的"),n("strong",[t._v("Goner")]),t._v("实例化后并加入到"),n("strong",[t._v("Cemetery")]),t._v("中:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("example"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("在上面的代码中,我们看到"),n("code",[t._v("gone.Run")]),t._v("可以接收形式如 "),n("strong",[n("code",[t._v("func (cemetery gone.Cemetery) error")])]),t._v(" 的函数;实际上这个函数,我们称之为 "),n("strong",[t._v("Priest")]),t._v(",是牧师的意思,他专门负责将 "),n("strong",[t._v("Goner")]),t._v(" 埋葬到 墓地("),n("strong",[t._v("Cemetery")]),t._v(")。")]),t._v(" "),n("h3",{attrs:{id:"如何执行goner中的业务代码"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#如何执行goner中的业务代码"}},[t._v("#")]),t._v(" 如何执行"),n("strong",[t._v("Goner")]),t._v("中的业务代码?")]),t._v(" "),n("p",[t._v("在Gone中,我们做了一个有趣的定义:Goner所有属性都被注入后,我们称这个Goner被复活了("),n("strong",[t._v("Revive")]),t._v(")。如果你查看Gone的源代码,你会发现管理复活Goner状态的组件叫"),n("strong",[t._v("Heaven")]),t._v("("),n("strong",[t._v("天堂")]),t._v("),这里灵感来源于各宗教神话中人死后会上天堂的传说。")]),t._v(" "),n("p",[t._v("为了执行"),n("strong",[t._v("Goner")]),t._v("业务代码,我们可以在"),n("strong",[t._v("Goner")]),t._v("上定义了方法 "),n("strong",[n("code",[t._v("AfterRevive() gone.AfterReviveError")])]),t._v(",这个方法会在"),n("strong",[t._v("Goner")]),t._v("被 "),n("strong",[t._v("Revive")]),t._v(" 后得到执行,并且我们将拥有该方法的 "),n("strong",[t._v("Goner")]),t._v(" 称之为 "),n("strong",[t._v("Prophet")]),t._v("(也就是 "),n("strong",[t._v("先知")]),t._v(")。实际上,一般情况我们只需要定义少量的先知来引导代码执行就可以了。")]),t._v(" "),n("p",[t._v("下面是一个例子;代码可以在"),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/after-revive/main.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),n("OutboundLink")],1),t._v("找到:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive 复活后执行的函数")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("执行上面代码,将得到结果:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Computer\nRevive main/Adder\nI want to compute"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("add")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),t._v(" is "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3000")]),t._v("\n")])])]),n("h3",{attrs:{id:"gone命令-自动生成priest函数"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#gone命令-自动生成priest函数"}},[t._v("#")]),t._v(" gone命令,自动生成"),n("code",[t._v("Priest")]),t._v("函数")]),t._v(" "),n("p",[t._v("实际上,前面已经讲完了Gone框架的核心功能;然而由于Golang本身的问题,我们无法做到像Spring那么方便,需要手动把所有 "),n("strong",[n("code",[t._v("Goner")])]),t._v(" 加入("),n("strong",[n("code",[t._v("Bury")])]),t._v(")到 "),n("strong",[n("code",[t._v("Cemetery")])]),t._v("。为了让Gone使用起来更方便,我们编写了一个辅助工具来自动生成 "),n("strong",[n("code",[t._v("Priest")])]),t._v(" 函数。下面介绍如何在一个项目中使用这个辅助工具。")]),t._v(" "),n("blockquote",[n("p",[t._v("完整代码可以在"),n("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/example/gen-code",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),n("OutboundLink")],1),t._v("找到")])]),t._v(" "),n("h4",{attrs:{id:"_1-安装辅助工具-gone"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-安装辅助工具-gone"}},[t._v("#")]),t._v(" 1. 安装辅助工具: gone")]),t._v(" "),n("p",[t._v("辅助工具和Gone框架同名,也叫gone,可以使用"),n("code",[t._v("go install")]),t._v("进行安装,如下:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),n("p",[t._v("gone 命令的使用可以参考:"),n("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),n("OutboundLink")],1)]),t._v(" "),n("h4",{attrs:{id:"_2-创建一个名为gen-code的新项目"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-创建一个名为gen-code的新项目"}},[t._v("#")]),t._v(" 2. 创建一个名为"),n("code",[t._v("gen-code")]),t._v("的新项目")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" gen-code\n"),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" gen-code\ngo mod init gen-code\n")])])]),n("h4",{attrs:{id:"_3-创建goner"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-创建goner"}},[t._v("#")]),t._v(" 3. 创建Goner")]),t._v(" "),n("p",[t._v("文件名:goner.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Adder "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a2 "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" a2\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Computer "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tadder Adder "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I want to compute!"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"1000 add 2000 is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("adder"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Add")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive 复活后执行的函数")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Computer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// boot")]),t._v("\n\tc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Compute")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("在上面代码中,请注意我们添加了两个工厂函数 "),n("code",[t._v("NewAdder() gone.Goner")]),t._v(" 和 "),n("code",[t._v("func NewComputer() gone.Goner")]),t._v(",并且在函数前做了一个特殊的注释:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n")])])]),n("p",[t._v("请不要删除这个注释,这个注释的作用是告诉辅助工具如何生成代码的。")]),t._v(" "),n("h4",{attrs:{id:"_4-使用辅助工具"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-使用辅助工具"}},[t._v("#")]),t._v(" 4. 使用辅助工具")]),t._v(" "),n("p",[t._v("请在"),n("code",[t._v("gen-code")]),t._v("目录下执行下面命令:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" main "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go\n")])])]),n("p",[t._v("这个命令的含义是,扫描当前目录生成一个 "),n("strong",[t._v("牧师")]),t._v(" 函数,它的函数名为 "),n("code",[t._v("Priest")]),t._v(",所在的包名为"),n("code",[t._v("main")]),t._v(",代码放到名为"),n("code",[t._v("priest.go")]),t._v("的文件中。\n代码执行完后,会在当前目录中生成一个文件"),n("code",[t._v("priest.go")]),t._v(",它的内容如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewAdder")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewComputer")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h4",{attrs:{id:"_5-添加main函数"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_5-添加main函数"}},[t._v("#")]),t._v(" 5. 添加"),n("code",[t._v("main")]),t._v("函数")]),t._v(" "),n("p",[t._v("文件名:main.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("至此,我们就完成了整个小的Gone程序,它的文件结构如下:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v("\n├── go.mod\n├── go.sum\n├── goner.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# goner的定义")]),t._v("\n├── main.go\n└── priest.go "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 生成的代码")]),t._v("\n")])])]),n("p",[t._v("可以通过命令"),n("code",[t._v("go run .")]),t._v("来执行,程序将输出如下内容:")]),t._v(" "),n("div",{staticClass:"language-code extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/Adder\nRevive main/Computer\nI want to compute!\n1000 add 2000 is 3000\n")])])]),n("h2",{attrs:{id:"匿名注入与具名注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#匿名注入与具名注入"}},[t._v("#")]),t._v(" 匿名注入与具名注入")]),t._v(" "),n("p",[t._v("在前面的篇章中,我们所有的例子实际上都是按类型的匿名注入;匿名注入时,如果存在多个同类型的"),n("strong",[t._v("Goner")]),t._v(",被注入只会是其中一个,通常是最先被复活的那个。在Gone中,是支持按名字注入的(也就是"),n("strong",[t._v("具名注入")]),t._v(")。")]),t._v(" "),n("p",[t._v("首先,"),n("strong",[n("code",[t._v("Cemetery.Bury")])]),t._v(" 函数的完整定义是这样的 "),n("strong",[n("code",[t._v("Bury(Goner, ...GonerId) Cemetery")])]),t._v(",这样的定义有两层考虑:")]),t._v(" "),n("ul",[n("li",[n("ol",[n("li",[t._v("用于支持"),n("strong",[t._v("具名埋葬")]),t._v(",第二个参数是可选的,允许传入一个字符串作为"),n("strong",[t._v("Goner")]),t._v("的"),n("strong",[t._v("ID")]),t._v("("),n("strong",[t._v("GonerId")]),t._v(");")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"2"}},[n("li",[t._v("使Bury函数支持链式调用。")])])])]),t._v(" "),n("h3",{attrs:{id:"具名埋葬"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#具名埋葬"}},[t._v("#")]),t._v(" 具名埋葬")]),t._v(" "),n("p",[t._v("实现"),n("strong",[t._v("具名埋葬")]),t._v(",我们的代码可以这样写:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬第一个AGoner,ID=A1")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬第二个AGoner,ID=A2")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("另外也可以这样写,这样写的好处是将Goner的构造和埋葬进行解耦:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 构造A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 构造A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest Responsible for putting Goners that need to be used into the framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("然后,就是结构体的具名注入,举个例子就能立刻明白:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\ta "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入一个AGoner")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入A1")]),t._v("\n\ta2 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入A2")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("注意:上面代码,结构体属性后的标签:")]),t._v(" "),n("ul",[n("li",[n("code",[t._v('gone:"*"')]),t._v("是按类型的"),n("strong",[t._v("匿名注入")]),t._v(";")]),t._v(" "),n("li",[n("code",[t._v('gone:"A1"')]),t._v("是注入"),n("code",[t._v("ID=A1")]),t._v("的AGoner的"),n("strong",[t._v("具名注入")]),t._v("。")])]),t._v(" "),n("p",[t._v("就是说,"),n("code",[t._v("gone")]),t._v("标签的值如果是"),n("code",[t._v("*")]),t._v("就是匿名注入;如果不是"),n("code",[t._v("*")]),t._v(",标签值就是要注入Goner的名字,也就是具名注入;当然,因为go是强类型的,所以无论 匿名注入 还是 具名注入 的 "),n("strong",[t._v("Goner")]),t._v(" 都必须是类型兼容的,否则注入失败。")]),t._v(" "),n("h2",{attrs:{id:"指针注入、值注入-和-接口注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#指针注入、值注入-和-接口注入"}},[t._v("#")]),t._v(" 指针注入、值注入 和 接口注入")]),t._v(" "),n("p",[t._v("如果被注入结构体的属性是一个指针,那么这个注入就是 "),n("strong",[t._v("指针注入")]),t._v(";"),n("strong",[t._v("值注入")]),t._v(" 和"),n("strong",[t._v("接口注入")]),t._v(" 的定义也是类似的。让我们来举个例子:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Speaker "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\ta0 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名注入一个AGoner; 指针注入")]),t._v("\n\ta1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入A1; 指针注入")]),t._v("\n\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n\ta3 Speaker "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A2"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 接口注入")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("上面代码中,"),n("code",[t._v("BGoner.a0")]),t._v(" 和 "),n("code",[t._v("BGoner.a1")]),t._v(" 是 "),n("strong",[t._v("指针注入")]),t._v(";"),n("code",[t._v("BGoner.a2")]),t._v("是值注入;"),n("code",[t._v("BGoner.a3")]),t._v("是 "),n("strong",[t._v("接口注入")]),t._v("。")]),t._v(" "),n("p",[n("strong",[t._v("需要特别提醒:")]),t._v("“在go语言中,"),n("strong",[t._v("值类型")]),t._v(" 的赋值和传参都是传递的拷贝”,这意味着我们如果使用"),n("strong",[t._v("值注入")]),t._v("时,实际上产生了一个新的“对象”,并且新旧对象只有在“传递那一刻”是相等,他们在内存中是独立的;这可能导致一些不符合“直觉”的结果,举个例子:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\ta1 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n\ta2 AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"A1"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 值注入")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"dapeng"')]),t._v("\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"wang"')]),t._v("\n\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"a1 is eq a2: %v"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a1 "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("a2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])]),n("p",[t._v("在上面的代码中,"),n("code",[t._v("BGoner.a1")]),t._v(" 和 "),n("code",[t._v("BGoner.a2")]),t._v(" 都被注入了 同一个 Goner("),n("code",[t._v("A1")]),t._v(" ),但是因为是"),n("strong",[t._v("值注入")]),t._v(",注入的过程中框架实际能做的只有将 "),n("code",[t._v("A1")]),t._v(" Goner 的值拷贝给了 "),n("code",[t._v("BGoner.a1")]),t._v(" 和 "),n("code",[t._v("BGoner.a2")]),t._v(";"),n("code",[t._v("BGoner.a1")]),t._v(" 和 "),n("code",[t._v("BGoner.a2")]),t._v("被注入后,就和"),n("code",[t._v("A1")]),t._v("没有了任何联系,"),n("code",[t._v("BGoner.a1")]),t._v(" 和 "),n("code",[t._v("BGoner.a2")]),t._v("之间也没有联系,内存中也会有三份"),n("code",[t._v("AGoner")]),t._v("类型的空间占用;"),n("code",[t._v('fmt.代码Printf("a1 is eq a2: %v", g.a1 == g.a2)')]),t._v("打印的结果也会是 "),n("code",[t._v("false")]),t._v("。")]),t._v(" "),n("blockquote",[n("p",[t._v("考虑到"),n("strong",[t._v("Gone")]),t._v("作为基础框架更多意义只是提供可能性,所以我们保留了 "),n("code",[t._v("值注入")]),t._v(" 功能;而"),n("strong",[t._v("大多数情况下,我们推荐使用 "),n("code",[t._v("指针注入")]),t._v(" 和 "),n("code",[t._v("接口注入")])]),t._v("。")])]),t._v(" "),n("h3",{attrs:{id:"指针注入-vs-接口注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#指针注入-vs-接口注入"}},[t._v("#")]),t._v(" 指针注入 vs 接口注入")]),t._v(" "),n("p",[t._v("在Goner的Bury过程中,要求传递的是一个引用,即"),n("code",[t._v("Cemetery.Bury")]),t._v("方法的第一个参数必须是引用类型。指针注入和接口注入都可以将 "),n("strong",[t._v("Bury")]),t._v(" 的引用传递给结构体的属性。"),n("strong",[t._v("指针注入")]),t._v(",简单直观,类型间一一对应,没有什么过多需要讲解的。接口(interface)做为go语言中最精华的设计之一,语言设计之初目的就在于 "),n("strong",[t._v("业务使用方和业务实现逻辑的解耦")]),t._v(",让接口的使用方不用关注实现的细节。接口的另一个作用是解除"),n("strong",[t._v("循环依赖")]),t._v(",如果两个模块间存在循环引用并且他们位于不同的包中;这样会导致package的循环依赖,在go语言中是禁止这样的行为的,编译阶段会失败。我们可以如下使用接口来解出package间的循环依赖。")]),t._v(" "),n("p",[t._v("循环依赖:"),n("br"),t._v(" "),n("img",{attrs:{src:s(256),alt:"循环依赖"}})]),t._v(" "),n("p",[t._v("使用接口解除循环依赖:"),n("br"),t._v(" "),n("img",{attrs:{src:s(257),alt:"使用接口解除循环依赖"}})]),t._v(" "),n("p",[t._v("使用接口能够隐藏业务逻辑的实现细节,能够有效的降低模块间的耦合,也更好的遵守“开放封闭”原则;因此,我们推荐使用 "),n("strong",[t._v("接口注入")]),t._v("。但是万事没有绝对,引入接口一定会增加额外的成本,所以我们还是支持了 "),n("strong",[t._v("指针注入")]),t._v("。")]),t._v(" "),n("h2",{attrs:{id:"slice注入和-map注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#slice注入和-map注入"}},[t._v("#")]),t._v(" Slice注入和 Map注入")]),t._v(" "),n("p",[t._v("Gone 支持对"),n("code",[t._v("Slice")]),t._v("和"),n("code",[t._v("Map")]),t._v("进行注入,即支持如下写法的:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[n("strong",[t._v("注入的规则如下:")])]),t._v(" "),n("ul",[n("li",[n("ol",[n("li",[t._v("Slice 和 Map 的元素类型可以是 Goner指针类型 和 Goner的值类型,也可以是一个接口;")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"2"}},[n("li",[t._v("Gone会将所有类型兼容的Goner注入到Slice 和 Map;")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"3"}},[n("li",[t._v("Map key的类型只能是string;")])])]),t._v(" "),n("li",[n("ol",{attrs:{start:"4"}},[n("li",[t._v("Map key的值为被注入Goner的GonerId,埋葬时没有指定GonerId的匿名Goner,Gone会自动生成一个Id。")])])])]),t._v(" "),n("div",{staticClass:"custom-block warning"},[n("p",{staticClass:"custom-block-title"},[t._v("注意")]),t._v(" "),n("p",[t._v("不推荐使用值作为Slice和Map的类型。")])]),t._v(" "),n("p",[t._v("下面是完整的例子:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" AGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//tell the framework that this struct is a Goner")]),t._v("\n\tName "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am the AGoner, My name is: %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BGoner "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\taSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针Slice")]),t._v("\n\taSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值Slice")]),t._v("\n\n\taMap1 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner指针的map")]),t._v("\n\taMap2 "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("AGoner "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入的属性为Goner值的map")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice1:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aSlice2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aSlice2:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap1 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap1[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("range")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("aMap2 "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"aMap2[%s]:%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" k"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" a"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA1 构造A1 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A1"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewA2 构造A2 AGoner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Injected Goner2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"A2"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("AGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Anonymous"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewA2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("BGoner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("例子执行的结果如下:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("Revive github.com/gone-io/gone/heaven\nRevive github.com/gone-io/gone/cemetery\nRevive main/AGoner\nRevive main/AGoner\nRevive main/AGoner\nRevive main/BGoner\naSlice1:I am the AGoner, My name is: Injected Goner1\naSlice1:I am the AGoner, My name is: Anonymous\naSlice1:I am the AGoner, My name is: Injected Goner2\n\naSlice2:I am the AGoner, My name is: Injected Goner1\naSlice2:I am the AGoner, My name is: Anonymous\naSlice2:I am the AGoner, My name is: Injected Goner2\n\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\naMap1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\n\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner2\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("A1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(":I am the AGoner, My name is: Injected Goner1\naMap2"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("main/AGoner"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#1374393662624]:I am the AGoner, My name is: Anonymous")]),t._v("\n")])])]),n("h2",{attrs:{id:"私有属性注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#私有属性注入"}},[t._v("#")]),t._v(" 私有属性注入")]),t._v(" "),n("p",[t._v("根据“开发封闭”原则,模块依赖的属性应该是私有的,Gone支持对结构体的私有变量注入。")]),t._v(" "),n("h2",{attrs:{id:"配置注入"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#配置注入"}},[t._v("#")]),t._v(" "),n("RouterLink",{attrs:{to:"/zh/guide/guide/config.html"}},[t._v("配置注入")])],1)])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/26.18203eb3.js b/assets/js/25.cb6cfcab.js similarity index 99% rename from assets/js/26.18203eb3.js rename to assets/js/25.cb6cfcab.js index b9af13a..eba2084 100644 --- a/assets/js/26.18203eb3.js +++ b/assets/js/25.cb6cfcab.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[26],{279:function(t,s,n){t.exports=n.p+"assets/img/image9.48d32078.png"},280:function(t,s,n){t.exports=n.p+"assets/img/image10.91a30027.png"},359:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-mysql"}},[t._v("#")]),t._v(" Web + MySQL")]),t._v(" "),s("p",[t._v("在本文中,我将演示如何创建一个可运行在生产环境的Web项目,并对项目做简单的介绍。项目的使用MySql作为数据库,使用docker-compose管理容器。")]),t._v(" "),s("h2",{attrs:{id:"安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#安装gone辅助工具"}},[t._v("#")]),t._v(" 安装gone辅助工具")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("关于gone命令,更多参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"使用gone命令创建项目"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用gone命令创建项目"}},[t._v("#")]),t._v(" 使用gone命令创建项目")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone create "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-t")]),t._v(" web+mysql web-mysql-docker\n")])])]),s("p",[t._v("上面命令会在当前目录中创建一个名为web-mysql-docker的目录。")]),t._v(" "),s("h2",{attrs:{id:"编译并运行"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#编译并运行"}},[t._v("#")]),t._v(" 编译并运行")]),t._v(" "),s("blockquote",[s("p",[t._v("假设你已经安装了"),s("code",[t._v("make")]),t._v(";如果没有安装请先安装,参考:"),s("a",{attrs:{href:"https://blog.csdn.net/waitdeng/article/details/138849321",target:"_blank",rel:"noopener noreferrer"}},[t._v("安装make"),s("OutboundLink")],1),t._v("。"),s("br"),t._v("\n建设你已经安装了"),s("code",[t._v("docker")]),t._v(" 和 "),s("code",[t._v("docker compose")]),t._v(";如果没有,参考: https://docs.docker.com/engine/install/。")])]),t._v(" "),s("p",[t._v("执行系列命令")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 进入项目目录")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-mysql-docker\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 生成Priest函数:https://goner.fun/zh/guide/auto-gen-priest.html")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 启动MySql服务")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker")]),t._v(" compose up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v(" mysql\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 编译并运行")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("p",[t._v("日志打印如下:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ web-mysql-docker "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" install-gone\ngo "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\ngo mod tidy\ngo generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\ngo run cmd/server/main.go\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/heaven\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/cemetery\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/tracer/tracer\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("local")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/default.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/local.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/xorm/engine\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/cmux/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/proxy\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/router\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/sysProcessor\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/responser\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/httpInjector\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/controller/demoController\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/AuthorizeMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/PubMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/db\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/demoService\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/authRouter\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/pubRouter\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" Running "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"debug"')]),t._v(" mode. Switch to "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"release"')]),t._v(" mode "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" production.\n - using env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("export")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token assign-left variable"}},[t._v("GIN_MODE")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v("release\n - using code: gin.SetMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gin.ReleaseMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/error --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/echo --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-query --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject/:key --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/inject-http-body --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-http-struct --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/page --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" PUT /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" DELETE /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/goner/gin/server.go:46"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Server Listen At :8080\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go:90"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/cemetery.go:329"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("Anonymous Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/works/gone-io/web-app/demo/web-mysql-docker/cmd/server/main.go:28"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("before start\n")])])]),s("h2",{attrs:{id:"测试接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#测试接口"}},[t._v("#")]),t._v(" 测试接口")]),t._v(" "),s("p",[t._v("我这里使用Goland内置Http Request插件测试。\n使用Goland打开项目,打开文件"),s("code",[t._v("tests/api/user.http")]),t._v(",选择"),s("code",[t._v("dev")]),t._v("环境运行,如下:\n"),s("img",{attrs:{src:n(279),alt:"运行"}})]),t._v(" "),s("p",[t._v("执行结果:\n"),s("img",{attrs:{src:n(280),alt:"结果"}})]),t._v(" "),s("h2",{attrs:{id:"项目简单介绍"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目简单介绍"}},[t._v("#")]),t._v(" 项目简单介绍")]),t._v(" "),s("h3",{attrs:{id:"目录结构"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#目录结构"}},[t._v("#")]),t._v(" 目录结构")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#项目main方法所在文件")]),t._v("\n├── config "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#项目配置目录")]),t._v("\n│ ├── default.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#默认配置")]),t._v("\n│ ├── dev.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#开发环境配置")]),t._v("\n│ ├── local.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#本地环境配置")]),t._v("\n│ └── prod.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#生产环境配置")]),t._v("\n├── docker-compose.yaml "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# docker-compose,定义了mysql 和 web 两个容器")]),t._v("\n├── go.mod\n├── internal\n│ ├── controller "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#Controller")]),t._v("\n│ │ └── demo_ctr.go\n│ ├── interface "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#接口目录")]),t._v("\n│ │ ├── domain "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#领域模型")]),t._v("\n│ │ │ ├── demo.go\n│ │ │ ├── page.go\n│ │ │ └── user.go\n│ │ ├── entity "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#实体模型")]),t._v("\n│ │ │ └── User.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#服务接口")]),t._v("\n│ │ └── i_demo.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#一个文件一般只放一个接口,文件名以`i_开口`,接口名以 `I`卡头,比如`IDemo`")]),t._v("\n│ ├── master.go\n│ ├── middleware "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#中间件目录,可以定义鉴权等统一处理逻辑")]),t._v("\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#模块目录")]),t._v("\n│ │ └── demo "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo模块")]),t._v("\n│ │ ├── db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo目录的数据库接口的实现")]),t._v("\n│ │ ├── demo_svc.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo Service,实现了`service.IDemo`接口")]),t._v("\n│ │ ├── error.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 当前目录错误码定义")]),t._v("\n│ │ └── i_db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo目录的数据库接口")]),t._v("\n│ ├── pkg "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#公共工具目录")]),t._v("\n│ │ └── utils\n│ │ └── error.go\n│ ├── priest.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#gone create 创建是没有该文件,运行 `make gone`生成的")]),t._v("\n│ └── router "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#路由目录")]),t._v("\n│ ├── auth_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 定义了需要鉴权的的路由组")]),t._v("\n│ └── pub_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 定义了无需鉴权的的路由组")]),t._v("\n├── scripts "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 脚本目录,用于存放一些脚本")]),t._v("\n│ └── mysql\n│ └── initdb.d "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#mysql initdb.d目录,docker-compose中定义的mysql首次启动时 会执行该目录下所有sql文件")]),t._v("\n│ └── user.sql\n└── tests "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#集成测试目录")]),t._v("\n └── api "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#接口测试目录")]),t._v("\n ├── demo.http\n ├── http-client.env.json\n └── user.http\n")])])]),s("h3",{attrs:{id:"项目特点"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目特点"}},[t._v("#")]),t._v(" 项目特点")]),t._v(" "),s("ul",[s("li",[t._v("开箱可用,无需额外配置直接可以运行")]),t._v(" "),s("li",[t._v("生成了"),s("code",[t._v("Dockerfile")]),t._v(",方便部署到各种容器环境")]),t._v(" "),s("li",[t._v("使用Makefile整合了gone、go、docker、docker compose等命令")]),t._v(" "),s("li",[t._v("自动生成Priest函数,使开发更顺畅")]),t._v(" "),s("li",[t._v("使用了gone框架,引入了依赖注入")]),t._v(" "),s("li",[t._v("使用接口解耦")]),t._v(" "),s("li",[t._v("支持分环境编写配置")]),t._v(" "),s("li",[t._v("集成了MySQL数据库")])]),t._v(" "),s("h3",{attrs:{id:"部分代码讲解"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#部分代码讲解"}},[t._v("#")]),t._v(" 部分代码讲解")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// demo数据 user 的增删改查,挂载到authRouter只为方便演示")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/users"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Body注入,根据contentType将HTTP正文解析为一个结构体,支持json、xml、form-data、x-www-form-urlencoded 等 contentType")]),t._v("\n\t\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ListUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/page"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tquery domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PageQuery "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Query注入")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PageUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("query"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PUT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Body注入")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UpdateUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DELETE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DeleteUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("上面代码截取于"),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v("文件。")]),t._v(" "),s("h3",{attrs:{id:"http-注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-注入"}},[t._v("#")]),t._v(" HTTP 注入")]),t._v(" "),s("p",[t._v("可以看到,"),s("strong",[t._v("Gone")]),t._v("在路由处理函数上也支持了"),s("strong",[t._v("依赖注入")]),t._v(";注入的方式是在使用一个匿名结构体作为处理函数的入参,并给结构体属性做特殊标记。\n上面代码用的注入标签:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v('gone:"http,body"')]),t._v(",Body注入,根据contentType将HTTP正文解析为一个结构体,支持json、xml、form-data、x-www-form-urlencoded 等 contentType,要求被注入的结构体属性的数据类型为"),s("strong",[t._v("结构体")]),t._v("或者"),s("strong",[t._v("结构体指针")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,query"')]),t._v(",Query注入,被注入的结构体属性的数据类型可以为"),s("strong",[t._v("结构体")]),t._v("、"),s("strong",[t._v("结构体指针")]),t._v("、"),s("strong",[t._v("字符串")]),t._v("、"),s("strong",[t._v("Number(int、uint、float64 ...)")]),t._v(" 和 "),s("strong",[t._v("字符串或Number的Slice")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,param"')]),t._v(",Url参数注入,将注入路由上定义的参数;要求被注入的结构体属性的数据类型为"),s("strong",[t._v("字符串")]),t._v("或"),s("strong",[t._v("Number类型")]),t._v("。")])]),t._v(" "),s("p",[t._v("下面对Query注入,句几个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Q "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Page "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用form标签指定参数名为page,默认是属性名Page")]),t._v("\n PageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize"`')]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用form标签指定参数名为pageSize,默认是属性名PageSize")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("handle")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n page "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//未指定query参数名,去属性名为key")]),t._v("\n thePage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=page"`')]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指定query参数名为page")]),t._v("\n size "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=pageSize`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指定query参数名为pageSize")]),t._v("\n\n q "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Q "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入类型为一个结构体")]),t._v("\n keywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keywords"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用数组结构,允许Query参数出现多次")]),t._v("\n oneKeyword "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keywords"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不使用数组,只获取Query中的第一个keyword")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("假设某个请求的Query为:"),s("code",[t._v("?page=10&pageSize=20&keywords=today&keywords=weather")]),t._v(",使用上面"),s("code",[t._v("handle")]),t._v("处理请求,参数"),s("code",[t._v("in")]),t._v("的值如下:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("in.page")]),t._v(" = 10")]),t._v(" "),s("li",[s("code",[t._v("in.thePage")]),t._v(" = 10")]),t._v(" "),s("li",[s("code",[t._v("in.size")]),t._v(" = 20")]),t._v(" "),s("li",[s("code",[t._v("in.q")]),t._v(" = {Page = 10, PageSize = 20}")]),t._v(" "),s("li",[s("code",[t._v("in.keywords")]),t._v(" = [today, weather]")]),t._v(" "),s("li",[s("code",[t._v("in.oneKeyword")]),t._v(" = today")])]),t._v(" "),s("p",[t._v("另外,处理函数还支持注入一些特殊结构体或指针(推荐使用指针):")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("gone.Content")]),t._v(" 请求上下文")]),t._v(" "),s("li",[s("code",[t._v("http.Request")]),t._v(" Http请求")]),t._v(" "),s("li",[s("code",[t._v("http.Header")]),t._v(" 请求头")]),t._v(" "),s("li",[s("code",[t._v("url.Url")]),t._v(" url")])]),t._v(" "),s("p",[t._v("框架还支持更多的注入标签和类型,请参考文档"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("http 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h3",{attrs:{id:"请求处理函数参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求处理函数参数"}},[t._v("#")]),t._v(" 请求处理函数参数")]),t._v(" "),s("p",[t._v("请求处理函数返回参数可以为如下几种形式:")]),t._v(" "),s("ol",[s("li",[t._v("不返回参数")]),t._v(" "),s("li",[t._v("返回一个非"),s("code",[t._v("error")]),t._v("的参数")]),t._v(" "),s("li",[t._v("返回数据和"),s("code",[t._v("error")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[25],{279:function(t,s,n){t.exports=n.p+"assets/img/image9.48d32078.png"},280:function(t,s,n){t.exports=n.p+"assets/img/image10.91a30027.png"},356:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-mysql"}},[t._v("#")]),t._v(" Web + MySQL")]),t._v(" "),s("p",[t._v("在本文中,我将演示如何创建一个可运行在生产环境的Web项目,并对项目做简单的介绍。项目的使用MySql作为数据库,使用docker-compose管理容器。")]),t._v(" "),s("h2",{attrs:{id:"安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#安装gone辅助工具"}},[t._v("#")]),t._v(" 安装gone辅助工具")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("关于gone命令,更多参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"使用gone命令创建项目"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用gone命令创建项目"}},[t._v("#")]),t._v(" 使用gone命令创建项目")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone create "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-t")]),t._v(" web+mysql web-mysql-docker\n")])])]),s("p",[t._v("上面命令会在当前目录中创建一个名为web-mysql-docker的目录。")]),t._v(" "),s("h2",{attrs:{id:"编译并运行"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#编译并运行"}},[t._v("#")]),t._v(" 编译并运行")]),t._v(" "),s("blockquote",[s("p",[t._v("假设你已经安装了"),s("code",[t._v("make")]),t._v(";如果没有安装请先安装,参考:"),s("a",{attrs:{href:"https://blog.csdn.net/waitdeng/article/details/138849321",target:"_blank",rel:"noopener noreferrer"}},[t._v("安装make"),s("OutboundLink")],1),t._v("。"),s("br"),t._v("\n建设你已经安装了"),s("code",[t._v("docker")]),t._v(" 和 "),s("code",[t._v("docker compose")]),t._v(";如果没有,参考: https://docs.docker.com/engine/install/。")])]),t._v(" "),s("p",[t._v("执行系列命令")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 进入项目目录")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-mysql-docker\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 生成Priest函数:https://goner.fun/zh/guide/auto-gen-priest.html")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 启动MySql服务")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker")]),t._v(" compose up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v(" mysql\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 编译并运行")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("p",[t._v("日志打印如下:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ web-mysql-docker "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" gone\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" install-gone\ngo "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\ngo mod tidy\ngo generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\ngo run cmd/server/main.go\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/heaven\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/cemetery\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/tracer/tracer\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/logrus/logger\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("==")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("local")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/default.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.574"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("properties: /var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build521033176/b001/exe/config/local.properties not found. skipping\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/config\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/config/propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/xorm/engine\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/cmux/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/proxy\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/router\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/sysProcessor\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/responser\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/server\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github.com/gone-io/gone/goner/gin/httpInjector\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/controller/demoController\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/AuthorizeMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/middleware/PubMiddleware\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/db\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/module/demo/demoService\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/authRouter\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.575"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive web-mysql/internal/router/pubRouter\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("WARNING"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" Running "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"debug"')]),t._v(" mode. Switch to "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"release"')]),t._v(" mode "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" production.\n - using env: "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("export")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token assign-left variable"}},[t._v("GIN_MODE")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v("release\n - using code: gin.SetMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gin.ReleaseMode"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/show --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/error --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/demo2/echo --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func5 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-query --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject/:key --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/inject-http-body --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/inject-http-struct --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" POST /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/page --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" GET /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" PUT /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("GIN-debug"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" DELETE /api/users/:id --"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v(" github.com/gone-io/gone/goner/gin."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".Proxy."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("*proxy"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(".proxyOne.func8 "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("7")]),t._v(" handlers"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/goner/gin/server.go:46"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Server Listen At :8080\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.576"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go:90"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.3.1/cemetery.go:329"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("Anonymous Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2024")]),t._v("-05-14 "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("12")]),t._v(":42:03.585"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("INFO"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("/Users/jim/works/gone-io/web-app/demo/web-mysql-docker/cmd/server/main.go:28"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("||")]),t._v("before start\n")])])]),s("h2",{attrs:{id:"测试接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#测试接口"}},[t._v("#")]),t._v(" 测试接口")]),t._v(" "),s("p",[t._v("我这里使用Goland内置Http Request插件测试。\n使用Goland打开项目,打开文件"),s("code",[t._v("tests/api/user.http")]),t._v(",选择"),s("code",[t._v("dev")]),t._v("环境运行,如下:\n"),s("img",{attrs:{src:n(279),alt:"运行"}})]),t._v(" "),s("p",[t._v("执行结果:\n"),s("img",{attrs:{src:n(280),alt:"结果"}})]),t._v(" "),s("h2",{attrs:{id:"项目简单介绍"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目简单介绍"}},[t._v("#")]),t._v(" 项目简单介绍")]),t._v(" "),s("h3",{attrs:{id:"目录结构"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#目录结构"}},[t._v("#")]),t._v(" 目录结构")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#项目main方法所在文件")]),t._v("\n├── config "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#项目配置目录")]),t._v("\n│ ├── default.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#默认配置")]),t._v("\n│ ├── dev.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#开发环境配置")]),t._v("\n│ ├── local.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#本地环境配置")]),t._v("\n│ └── prod.properties "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#生产环境配置")]),t._v("\n├── docker-compose.yaml "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# docker-compose,定义了mysql 和 web 两个容器")]),t._v("\n├── go.mod\n├── internal\n│ ├── controller "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#Controller")]),t._v("\n│ │ └── demo_ctr.go\n│ ├── interface "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#接口目录")]),t._v("\n│ │ ├── domain "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#领域模型")]),t._v("\n│ │ │ ├── demo.go\n│ │ │ ├── page.go\n│ │ │ └── user.go\n│ │ ├── entity "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#实体模型")]),t._v("\n│ │ │ └── User.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#服务接口")]),t._v("\n│ │ └── i_demo.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#一个文件一般只放一个接口,文件名以`i_开口`,接口名以 `I`卡头,比如`IDemo`")]),t._v("\n│ ├── master.go\n│ ├── middleware "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#中间件目录,可以定义鉴权等统一处理逻辑")]),t._v("\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#模块目录")]),t._v("\n│ │ └── demo "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo模块")]),t._v("\n│ │ ├── db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo目录的数据库接口的实现")]),t._v("\n│ │ ├── demo_svc.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo Service,实现了`service.IDemo`接口")]),t._v("\n│ │ ├── error.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 当前目录错误码定义")]),t._v("\n│ │ └── i_db.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#demo目录的数据库接口")]),t._v("\n│ ├── pkg "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#公共工具目录")]),t._v("\n│ │ └── utils\n│ │ └── error.go\n│ ├── priest.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#gone create 创建是没有该文件,运行 `make gone`生成的")]),t._v("\n│ └── router "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#路由目录")]),t._v("\n│ ├── auth_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 定义了需要鉴权的的路由组")]),t._v("\n│ └── pub_router.go "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 定义了无需鉴权的的路由组")]),t._v("\n├── scripts "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 脚本目录,用于存放一些脚本")]),t._v("\n│ └── mysql\n│ └── initdb.d "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#mysql initdb.d目录,docker-compose中定义的mysql首次启动时 会执行该目录下所有sql文件")]),t._v("\n│ └── user.sql\n└── tests "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#集成测试目录")]),t._v("\n └── api "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("#接口测试目录")]),t._v("\n ├── demo.http\n ├── http-client.env.json\n └── user.http\n")])])]),s("h3",{attrs:{id:"项目特点"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目特点"}},[t._v("#")]),t._v(" 项目特点")]),t._v(" "),s("ul",[s("li",[t._v("开箱可用,无需额外配置直接可以运行")]),t._v(" "),s("li",[t._v("生成了"),s("code",[t._v("Dockerfile")]),t._v(",方便部署到各种容器环境")]),t._v(" "),s("li",[t._v("使用Makefile整合了gone、go、docker、docker compose等命令")]),t._v(" "),s("li",[t._v("自动生成Priest函数,使开发更顺畅")]),t._v(" "),s("li",[t._v("使用了gone框架,引入了依赖注入")]),t._v(" "),s("li",[t._v("使用接口解耦")]),t._v(" "),s("li",[t._v("支持分环境编写配置")]),t._v(" "),s("li",[t._v("集成了MySQL数据库")])]),t._v(" "),s("h3",{attrs:{id:"部分代码讲解"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#部分代码讲解"}},[t._v("#")]),t._v(" 部分代码讲解")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// demo数据 user 的增删改查,挂载到authRouter只为方便演示")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/users"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Body注入,根据contentType将HTTP正文解析为一个结构体,支持json、xml、form-data、x-www-form-urlencoded 等 contentType")]),t._v("\n\t\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ListUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/page"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tquery domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PageQuery "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Query注入")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PageUsers")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("query"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("PUT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Body注入")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UpdateUserById")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DELETE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/:id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tid "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Url参数注入,将注入路由上定义的`:id`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DeleteUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("上面代码截取于"),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v("文件。")]),t._v(" "),s("h3",{attrs:{id:"http-注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-注入"}},[t._v("#")]),t._v(" HTTP 注入")]),t._v(" "),s("p",[t._v("可以看到,"),s("strong",[t._v("Gone")]),t._v("在路由处理函数上也支持了"),s("strong",[t._v("依赖注入")]),t._v(";注入的方式是在使用一个匿名结构体作为处理函数的入参,并给结构体属性做特殊标记。\n上面代码用的注入标签:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v('gone:"http,body"')]),t._v(",Body注入,根据contentType将HTTP正文解析为一个结构体,支持json、xml、form-data、x-www-form-urlencoded 等 contentType,要求被注入的结构体属性的数据类型为"),s("strong",[t._v("结构体")]),t._v("或者"),s("strong",[t._v("结构体指针")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,query"')]),t._v(",Query注入,被注入的结构体属性的数据类型可以为"),s("strong",[t._v("结构体")]),t._v("、"),s("strong",[t._v("结构体指针")]),t._v("、"),s("strong",[t._v("字符串")]),t._v("、"),s("strong",[t._v("Number(int、uint、float64 ...)")]),t._v(" 和 "),s("strong",[t._v("字符串或Number的Slice")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v('gone:"http,param"')]),t._v(",Url参数注入,将注入路由上定义的参数;要求被注入的结构体属性的数据类型为"),s("strong",[t._v("字符串")]),t._v("或"),s("strong",[t._v("Number类型")]),t._v("。")])]),t._v(" "),s("p",[t._v("下面对Query注入,句几个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Q "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n Page "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用form标签指定参数名为page,默认是属性名Page")]),t._v("\n PageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize"`')]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用form标签指定参数名为pageSize,默认是属性名PageSize")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("handle")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n page "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//未指定query参数名,去属性名为key")]),t._v("\n thePage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=page"`')]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指定query参数名为page")]),t._v("\n size "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=pageSize`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指定query参数名为pageSize")]),t._v("\n\n q "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Q "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被注入类型为一个结构体")]),t._v("\n keywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keywords"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用数组结构,允许Query参数出现多次")]),t._v("\n oneKeyword "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keywords"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不使用数组,只获取Query中的第一个keyword")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("假设某个请求的Query为:"),s("code",[t._v("?page=10&pageSize=20&keywords=today&keywords=weather")]),t._v(",使用上面"),s("code",[t._v("handle")]),t._v("处理请求,参数"),s("code",[t._v("in")]),t._v("的值如下:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("in.page")]),t._v(" = 10")]),t._v(" "),s("li",[s("code",[t._v("in.thePage")]),t._v(" = 10")]),t._v(" "),s("li",[s("code",[t._v("in.size")]),t._v(" = 20")]),t._v(" "),s("li",[s("code",[t._v("in.q")]),t._v(" = {Page = 10, PageSize = 20}")]),t._v(" "),s("li",[s("code",[t._v("in.keywords")]),t._v(" = [today, weather]")]),t._v(" "),s("li",[s("code",[t._v("in.oneKeyword")]),t._v(" = today")])]),t._v(" "),s("p",[t._v("另外,处理函数还支持注入一些特殊结构体或指针(推荐使用指针):")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("gone.Content")]),t._v(" 请求上下文")]),t._v(" "),s("li",[s("code",[t._v("http.Request")]),t._v(" Http请求")]),t._v(" "),s("li",[s("code",[t._v("http.Header")]),t._v(" 请求头")]),t._v(" "),s("li",[s("code",[t._v("url.Url")]),t._v(" url")])]),t._v(" "),s("p",[t._v("框架还支持更多的注入标签和类型,请参考文档"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("http 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h3",{attrs:{id:"请求处理函数参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求处理函数参数"}},[t._v("#")]),t._v(" 请求处理函数参数")]),t._v(" "),s("p",[t._v("请求处理函数返回参数可以为如下几种形式:")]),t._v(" "),s("ol",[s("li",[t._v("不返回参数")]),t._v(" "),s("li",[t._v("返回一个非"),s("code",[t._v("error")]),t._v("的参数")]),t._v(" "),s("li",[t._v("返回数据和"),s("code",[t._v("error")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/27.f64ac172.js b/assets/js/26.2c1823fd.js similarity index 98% rename from assets/js/27.f64ac172.js rename to assets/js/26.2c1823fd.js index 89c3e09..0d0f838 100644 --- a/assets/js/27.f64ac172.js +++ b/assets/js/26.2c1823fd.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[27],{242:function(t,n,e){"use strict";e.d(n,"d",(function(){return r})),e.d(n,"a",(function(){return s})),e.d(n,"i",(function(){return u})),e.d(n,"f",(function(){return a})),e.d(n,"g",(function(){return l})),e.d(n,"h",(function(){return c})),e.d(n,"b",(function(){return f})),e.d(n,"e",(function(){return h})),e.d(n,"k",(function(){return p})),e.d(n,"l",(function(){return d})),e.d(n,"c",(function(){return b})),e.d(n,"j",(function(){return m}));e(91);const r=/#.*$/,i=/\.(md|html)$/,s=/\/$/,u=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(r,"").replace(i,"")}function a(t){return u.test(t)}function l(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function f(t){if(a(t))return t;const n=t.match(r),e=n?n[0]:"",i=o(t);return s.test(i)?t:i+".html"+e}function h(t,n){const e=decodeURIComponent(t.hash),i=function(t){const n=t.match(r);if(n)return n[0]}(n);if(i&&e!==i)return!1;return o(t.path)===o(n)}function p(t,n,e){if(a(n))return{type:"external",path:n};e&&(n=function(t,n,e){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return n+t;const i=n.split("/");e&&i[i.length-1]||i.pop();const s=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(n,e,r,i=1){if("string"==typeof n)return p(e,n,r);if(Array.isArray(n))return Object.assign(p(e,n[0],r),{title:n[1]});{const s=n.children||[];return 0===s.length&&n.path?Object.assign(p(e,n.path,r),{title:n.title}):{type:"group",path:n.path,title:n.title,sidebarDepth:n.sidebarDepth,initialOpenGroupIndex:n.initialOpenGroupIndex,children:s.map(n=>t(n,e,r,i+1)),collapsable:!1!==n.collapsable}}}(t,i,e)):[]}return[]}function g(t){const n=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:n.map(n=>({type:"auto",title:n.title,basePath:t.path,path:t.path+"#"+n.slug,children:n.children||[]}))}]}function b(t){let n;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?n=t:n&&(n.children||(n.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},244:function(t,n,e){"use strict";e.r(n);var r=e(242),i={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(r.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(r.g)(this.link)||Object(r.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(r.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(r.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=e(10),u=Object(s.a)(i,(function(){var t=this,n=t._self._c;return t.isInternal?n("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(n){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):n("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?n("OutboundLink"):t._e()],1)}),[],!1,null,null,null);n.default=u.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[26],{242:function(t,n,e){"use strict";e.d(n,"d",(function(){return r})),e.d(n,"a",(function(){return s})),e.d(n,"i",(function(){return u})),e.d(n,"f",(function(){return a})),e.d(n,"g",(function(){return l})),e.d(n,"h",(function(){return c})),e.d(n,"b",(function(){return f})),e.d(n,"e",(function(){return h})),e.d(n,"k",(function(){return p})),e.d(n,"l",(function(){return d})),e.d(n,"c",(function(){return b})),e.d(n,"j",(function(){return m}));e(91);const r=/#.*$/,i=/\.(md|html)$/,s=/\/$/,u=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(r,"").replace(i,"")}function a(t){return u.test(t)}function l(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function f(t){if(a(t))return t;const n=t.match(r),e=n?n[0]:"",i=o(t);return s.test(i)?t:i+".html"+e}function h(t,n){const e=decodeURIComponent(t.hash),i=function(t){const n=t.match(r);if(n)return n[0]}(n);if(i&&e!==i)return!1;return o(t.path)===o(n)}function p(t,n,e){if(a(n))return{type:"external",path:n};e&&(n=function(t,n,e){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return n+t;const i=n.split("/");e&&i[i.length-1]||i.pop();const s=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(n,e,r,i=1){if("string"==typeof n)return p(e,n,r);if(Array.isArray(n))return Object.assign(p(e,n[0],r),{title:n[1]});{const s=n.children||[];return 0===s.length&&n.path?Object.assign(p(e,n.path,r),{title:n.title}):{type:"group",path:n.path,title:n.title,sidebarDepth:n.sidebarDepth,initialOpenGroupIndex:n.initialOpenGroupIndex,children:s.map(n=>t(n,e,r,i+1)),collapsable:!1!==n.collapsable}}}(t,i,e)):[]}return[]}function g(t){const n=b(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:n.map(n=>({type:"auto",title:n.title,basePath:t.path,path:t.path+"#"+n.slug,children:n.children||[]}))}]}function b(t){let n;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?n=t:n&&(n.children||(n.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},244:function(t,n,e){"use strict";e.r(n);var r=e(242),i={name:"NavLink",props:{item:{required:!0}},computed:{link(){return Object(r.b)(this.item.link)},exact(){return this.$site.locales?Object.keys(this.$site.locales).some(t=>t===this.link):"/"===this.link},isNonHttpURI(){return Object(r.g)(this.link)||Object(r.h)(this.link)},isBlankTarget(){return"_blank"===this.target},isInternal(){return!Object(r.f)(this.link)&&!this.isBlankTarget},target(){return this.isNonHttpURI?null:this.item.target?this.item.target:Object(r.f)(this.link)?"_blank":""},rel(){return this.isNonHttpURI||!1===this.item.rel?null:this.item.rel?this.item.rel:this.isBlankTarget?"noopener noreferrer":null}},methods:{focusoutAction(){this.$emit("focusout")}}},s=e(10),u=Object(s.a)(i,(function(){var t=this,n=t._self._c;return t.isInternal?n("RouterLink",{staticClass:"nav-link",attrs:{to:t.link,exact:t.exact},nativeOn:{focusout:function(n){return t.focusoutAction.apply(null,arguments)}}},[t._v("\n "+t._s(t.item.text)+"\n")]):n("a",{staticClass:"nav-link external",attrs:{href:t.link,target:t.target,rel:t.rel},on:{focusout:t.focusoutAction}},[t._v("\n "+t._s(t.item.text)+"\n "),t.isBlankTarget?n("OutboundLink"):t._e()],1)}),[],!1,null,null,null);n.default=u.exports}}]); \ No newline at end of file diff --git a/assets/js/28.f55cfb0c.js b/assets/js/27.19e0637a.js similarity index 99% rename from assets/js/28.f55cfb0c.js rename to assets/js/27.19e0637a.js index e584614..9efb149 100644 --- a/assets/js/28.f55cfb0c.js +++ b/assets/js/27.19e0637a.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[28],{302:function(t,n,s){t.exports=s.p+"assets/img/image.01b2ecda.png"},334:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"v1-x版本-正式发布"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#v1-x版本-正式发布"}},[t._v("#")]),t._v(" v1.x版本 正式发布")]),t._v(" "),n("h2",{attrs:{id:"优化和新特性"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#优化和新特性"}},[t._v("#")]),t._v(" 优化和新特性")]),t._v(" "),n("h3",{attrs:{id:"gone-核心功能增强"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#gone-核心功能增强"}},[t._v("#")]),t._v(" gone 核心功能增强")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("重构了函数参数依赖注入\n在"),n("strong",[t._v("Cemetery")]),t._v("上提供了"),n("code",[t._v("InjectFuncParameters")]),t._v("方法,用于完成函数参数的依赖注入。")]),t._v(" "),n("p",[t._v("该方法的定义如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("InjectFuncParameters")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\tfn any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\tinjectBefore "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pt reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Type"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\tinjectAfter "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pt reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Type"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("ul",[n("li",[t._v("入参说明:\n"),n("ul",[n("li",[t._v("fn,需要被注入的函数;函数允许拥有多个入参,入数可以是Gone框架中注册的接口或者结构体指针,也可以为被"),n("code",[t._v("gone")]),t._v("标记了属性的结构体,一般使用匿名结构体;")]),t._v(" "),n("li",[t._v("injectBefore,hook函数,在对第i个参数构造前调用,如果"),n("code",[t._v("injectBefore(x, i)")]),t._v("返回值非nil,InjectFuncParameters将不再构造fn函数的第"),n("code",[t._v("i")]),t._v("个参数,而是将该值的"),n("code",[t._v("reflect.Value")]),t._v("直接作为args数组的第i个值;")]),t._v(" "),n("li",[t._v("injectAfter,hook函数,在对第i个参数成功构造后调用;")])])]),t._v(" "),n("li",[t._v("出参说明\n"),n("ul",[n("li",[t._v("args,fn参数的"),n("code",[t._v("reflect.Value")]),t._v("数组")]),t._v(" "),n("li",[t._v("err,函数构造返回的错误")])])]),t._v(" "),n("li",[t._v("功能说明:\n根据fn函数的定义和Gone框架中注册的Goners,自动构造fn的参数数组args。然后可以使用"),n("code",[t._v("func (v Value) Call(in []Value) []Value")]),t._v("对函数进行调用。")])])]),t._v(" "),n("li",[n("p",[t._v("引入了"),n("strong",[t._v("gone.GonerOption")]),t._v("接口,用于给"),n("code",[t._v("Bury")]),t._v("提供选项。")]),t._v(" "),n("ul",[n("li",[t._v("gone.GonerId,在Bury时给Goner提供GonerId")]),t._v(" "),n("li",[t._v("gone.Order,在Bury时给Goner提供一个序号;可以用于明确定义Goners的加载顺序和hook函数的执行顺序。")]),t._v(" "),n("li",[t._v("gone.IsDefault,在Bury时给Goner提供默认的接口类型,后续我们将另外些一篇博客介绍该特性。")])])]),t._v(" "),n("li",[n("p",[t._v("调整了"),n("strong",[t._v("Cemetery")]),t._v("上"),n("code",[t._v("Bury*")]),t._v("方法支持gone.GonerOption接口,包括:")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("Bury(Goner, ...GonerOption) Cemetery")]),t._v(",")]),t._v(" "),n("li",[n("code",[t._v("BuryOnce(goner Goner, options ...GonerOption) Cemetery")])]),t._v(" "),n("li",[n("code",[t._v("ReplaceBury(goner Goner, options ...GonerOption) error")])])])]),t._v(" "),n("li",[n("p",[t._v("重构了"),n("strong",[t._v("gone.Preparer")])]),t._v(" "),n("ul",[n("li",[n("code",[t._v("Preparer.Run")]),t._v("和"),n("code",[t._v("Preparer.Serve")]),t._v("方法,支持传入函数作为参数在框架启动后执行,可以传0个或者多个,函数支持依赖注入;")]),t._v(" "),n("li",[n("code",[t._v("Preparer.BeforeStart")]),t._v(", "),n("code",[t._v("Preparer.AfterStart")]),t._v(", "),n("code",[t._v("Preparer.BeforeStop")]),t._v(", "),n("code",[t._v("Preparer.AfterStop")]),t._v(" 注册的hook函数,支持依赖注入;")]),t._v(" "),n("li",[t._v("添加"),n("code",[t._v("Preparer.Test")]),t._v("方法,用于编写测试代码。")])])]),t._v(" "),n("li",[n("p",[t._v("基于 "),n("strong",[t._v("gone.Preparer")]),t._v(" 重构了 TestKit的实现,可以使用下面三种方式之一编写测试代码:")]),t._v(" "),n("ul",[n("li",[n("p",[n("code",[t._v("gone.Test")]),t._v(" 测试某种类型的Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[n("code",[t._v("gone.TestAt")]),t._v(",测试某个具名的Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v("\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[n("code",[t._v("gone.Prepare().Test")]),t._v(",Prepare上的Test方法,支持传入一个函数作为参数,函数的参数将会被自动注入。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n \t\tline "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入gone框架中注册的类型")]),t._v("\n\n \t\tin "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入匿名结构体")]),t._v("\n \t\t\tpoint "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n \t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])])])]),t._v(" "),n("h3",{attrs:{id:"内置goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#内置goners"}},[t._v("#")]),t._v(" 内置Goners")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("重构"),n("code",[t._v("goner/gin")]),t._v("的HTTP请求参数注入机制。")]),t._v(" "),n("ul",[n("li",[t._v("使用"),n("code",[t._v("Cemetery.InjectFuncParameters")]),t._v("方法,对处理请求时的依赖注入机制进行重构。")]),t._v(" "),n("li",[t._v("在v0.x的版本中,处理HTTP请求注入的方式时,每次请求时都会创建一个函数来处理注入;在v1.x的版本中,会在程序启动流程中一次性构建处理函数,每次处理HTTP请求时的性能损耗和gin的性能差异基本一致。")]),t._v(" "),n("li",[t._v("HTTP请求参数的依赖注入,由于使用InjectFuncParameters重构,得到了增强:可以支持多个入参,入参可以是Gone框架中注册的接口或者结构体指针,也可以为被"),n("code",[t._v("gone")]),t._v("标记了属性的结构体,一般使用匿名结构体。")])])]),t._v(" "),n("li",[n("p",[t._v("重构 "),n("code",[t._v("goner/config")]),t._v(",优化代码实现,将接口"),n("code",[t._v("Configure")]),t._v("从"),n("code",[t._v("goner/config")]),t._v("包中提到"),n("code",[t._v("gone")]),t._v("包中。")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("gone.Configure")]),t._v("接口用于从抽象的设备中读取配置项,支持实现使用其他方式读取配置,如从文件、数据库等。")]),t._v(" "),n("li",[t._v("从"),n("code",[t._v(".properties")]),t._v("文件从读取配置,作为默认的配置读取方式,依赖保留在"),n("code",[t._v("goner/config")]),t._v("包中。")])])]),t._v(" "),n("li",[n("p",[t._v("新增加"),n("code",[t._v("goner/viper")]),t._v(",实现了"),n("code",[t._v("gone.Configure")]),t._v("接口,支持从多种配置文件读取配置。")])]),t._v(" "),n("li",[n("p",[t._v("新增加"),n("code",[t._v("goner/zap")]),t._v(",集成了"),n("code",[t._v("go.uber.org/zap")]),t._v(",用于日志输出。")]),t._v(" "),n("ul",[n("li",[t._v("日志接口"),n("code",[t._v("Logger")]),t._v("从"),n("code",[t._v("logrus.Logger")]),t._v("包移到"),n("code",[t._v("gone")]),t._v("包中。")]),t._v(" "),n("li",[t._v("使用日志,只需要注入"),n("code",[t._v("gone.Logger")]),t._v("即可;无需关注底层使用的何种日志框架。")])])])]),t._v(" "),n("h3",{attrs:{id:"覆盖测试"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#覆盖测试"}},[t._v("#")]),t._v(" 覆盖测试")]),t._v(" "),n("p",[t._v("对gone的代码编写了大量的单元测试代码,覆盖率达到90%。")]),t._v(" "),n("p",[n("a",{attrs:{href:"https://app.codecov.io/gh/gone-io/gone",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:s(302),alt:"test"}}),n("OutboundLink")],1)]),t._v(" "),n("h2",{attrs:{id:"后续计划"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#后续计划"}},[t._v("#")]),t._v(" 后续计划")]),t._v(" "),n("ol",[n("li",[t._v("提供更多的开箱可用的Goners,接入更多的优秀的Web中间件。")]),t._v(" "),n("li",[t._v("编写一个完善的参考案例")])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[27],{302:function(t,n,s){t.exports=s.p+"assets/img/image.01b2ecda.png"},333:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"v1-x版本-正式发布"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#v1-x版本-正式发布"}},[t._v("#")]),t._v(" v1.x版本 正式发布")]),t._v(" "),n("h2",{attrs:{id:"优化和新特性"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#优化和新特性"}},[t._v("#")]),t._v(" 优化和新特性")]),t._v(" "),n("h3",{attrs:{id:"gone-核心功能增强"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#gone-核心功能增强"}},[t._v("#")]),t._v(" gone 核心功能增强")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("重构了函数参数依赖注入\n在"),n("strong",[t._v("Cemetery")]),t._v("上提供了"),n("code",[t._v("InjectFuncParameters")]),t._v("方法,用于完成函数参数的依赖注入。")]),t._v(" "),n("p",[t._v("该方法的定义如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("InjectFuncParameters")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\tfn any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\tinjectBefore "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pt reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Type"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\tinjectAfter "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pt reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Type"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" i "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("reflect"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("ul",[n("li",[t._v("入参说明:\n"),n("ul",[n("li",[t._v("fn,需要被注入的函数;函数允许拥有多个入参,入数可以是Gone框架中注册的接口或者结构体指针,也可以为被"),n("code",[t._v("gone")]),t._v("标记了属性的结构体,一般使用匿名结构体;")]),t._v(" "),n("li",[t._v("injectBefore,hook函数,在对第i个参数构造前调用,如果"),n("code",[t._v("injectBefore(x, i)")]),t._v("返回值非nil,InjectFuncParameters将不再构造fn函数的第"),n("code",[t._v("i")]),t._v("个参数,而是将该值的"),n("code",[t._v("reflect.Value")]),t._v("直接作为args数组的第i个值;")]),t._v(" "),n("li",[t._v("injectAfter,hook函数,在对第i个参数成功构造后调用;")])])]),t._v(" "),n("li",[t._v("出参说明\n"),n("ul",[n("li",[t._v("args,fn参数的"),n("code",[t._v("reflect.Value")]),t._v("数组")]),t._v(" "),n("li",[t._v("err,函数构造返回的错误")])])]),t._v(" "),n("li",[t._v("功能说明:\n根据fn函数的定义和Gone框架中注册的Goners,自动构造fn的参数数组args。然后可以使用"),n("code",[t._v("func (v Value) Call(in []Value) []Value")]),t._v("对函数进行调用。")])])]),t._v(" "),n("li",[n("p",[t._v("引入了"),n("strong",[t._v("gone.GonerOption")]),t._v("接口,用于给"),n("code",[t._v("Bury")]),t._v("提供选项。")]),t._v(" "),n("ul",[n("li",[t._v("gone.GonerId,在Bury时给Goner提供GonerId")]),t._v(" "),n("li",[t._v("gone.Order,在Bury时给Goner提供一个序号;可以用于明确定义Goners的加载顺序和hook函数的执行顺序。")]),t._v(" "),n("li",[t._v("gone.IsDefault,在Bury时给Goner提供默认的接口类型,后续我们将另外些一篇博客介绍该特性。")])])]),t._v(" "),n("li",[n("p",[t._v("调整了"),n("strong",[t._v("Cemetery")]),t._v("上"),n("code",[t._v("Bury*")]),t._v("方法支持gone.GonerOption接口,包括:")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("Bury(Goner, ...GonerOption) Cemetery")]),t._v(",")]),t._v(" "),n("li",[n("code",[t._v("BuryOnce(goner Goner, options ...GonerOption) Cemetery")])]),t._v(" "),n("li",[n("code",[t._v("ReplaceBury(goner Goner, options ...GonerOption) error")])])])]),t._v(" "),n("li",[n("p",[t._v("重构了"),n("strong",[t._v("gone.Preparer")])]),t._v(" "),n("ul",[n("li",[n("code",[t._v("Preparer.Run")]),t._v("和"),n("code",[t._v("Preparer.Serve")]),t._v("方法,支持传入函数作为参数在框架启动后执行,可以传0个或者多个,函数支持依赖注入;")]),t._v(" "),n("li",[n("code",[t._v("Preparer.BeforeStart")]),t._v(", "),n("code",[t._v("Preparer.AfterStart")]),t._v(", "),n("code",[t._v("Preparer.BeforeStop")]),t._v(", "),n("code",[t._v("Preparer.AfterStop")]),t._v(" 注册的hook函数,支持依赖注入;")]),t._v(" "),n("li",[t._v("添加"),n("code",[t._v("Preparer.Test")]),t._v("方法,用于编写测试代码。")])])]),t._v(" "),n("li",[n("p",[t._v("基于 "),n("strong",[t._v("gone.Preparer")]),t._v(" 重构了 TestKit的实现,可以使用下面三种方式之一编写测试代码:")]),t._v(" "),n("ul",[n("li",[n("p",[n("code",[t._v("gone.Test")]),t._v(" 测试某种类型的Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[n("code",[t._v("gone.TestAt")]),t._v(",测试某个具名的Goner:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v("\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[n("code",[t._v("gone.Prepare().Test")]),t._v(",Prepare上的Test方法,支持传入一个函数作为参数,函数的参数将会被自动注入。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n \t\tline "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入gone框架中注册的类型")]),t._v("\n\n \t\tin "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入匿名结构体")]),t._v("\n \t\t\tpoint "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n \t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n \t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n \t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])])])]),t._v(" "),n("h3",{attrs:{id:"内置goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#内置goners"}},[t._v("#")]),t._v(" 内置Goners")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("重构"),n("code",[t._v("goner/gin")]),t._v("的HTTP请求参数注入机制。")]),t._v(" "),n("ul",[n("li",[t._v("使用"),n("code",[t._v("Cemetery.InjectFuncParameters")]),t._v("方法,对处理请求时的依赖注入机制进行重构。")]),t._v(" "),n("li",[t._v("在v0.x的版本中,处理HTTP请求注入的方式时,每次请求时都会创建一个函数来处理注入;在v1.x的版本中,会在程序启动流程中一次性构建处理函数,每次处理HTTP请求时的性能损耗和gin的性能差异基本一致。")]),t._v(" "),n("li",[t._v("HTTP请求参数的依赖注入,由于使用InjectFuncParameters重构,得到了增强:可以支持多个入参,入参可以是Gone框架中注册的接口或者结构体指针,也可以为被"),n("code",[t._v("gone")]),t._v("标记了属性的结构体,一般使用匿名结构体。")])])]),t._v(" "),n("li",[n("p",[t._v("重构 "),n("code",[t._v("goner/config")]),t._v(",优化代码实现,将接口"),n("code",[t._v("Configure")]),t._v("从"),n("code",[t._v("goner/config")]),t._v("包中提到"),n("code",[t._v("gone")]),t._v("包中。")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("gone.Configure")]),t._v("接口用于从抽象的设备中读取配置项,支持实现使用其他方式读取配置,如从文件、数据库等。")]),t._v(" "),n("li",[t._v("从"),n("code",[t._v(".properties")]),t._v("文件从读取配置,作为默认的配置读取方式,依赖保留在"),n("code",[t._v("goner/config")]),t._v("包中。")])])]),t._v(" "),n("li",[n("p",[t._v("新增加"),n("code",[t._v("goner/viper")]),t._v(",实现了"),n("code",[t._v("gone.Configure")]),t._v("接口,支持从多种配置文件读取配置。")])]),t._v(" "),n("li",[n("p",[t._v("新增加"),n("code",[t._v("goner/zap")]),t._v(",集成了"),n("code",[t._v("go.uber.org/zap")]),t._v(",用于日志输出。")]),t._v(" "),n("ul",[n("li",[t._v("日志接口"),n("code",[t._v("Logger")]),t._v("从"),n("code",[t._v("logrus.Logger")]),t._v("包移到"),n("code",[t._v("gone")]),t._v("包中。")]),t._v(" "),n("li",[t._v("使用日志,只需要注入"),n("code",[t._v("gone.Logger")]),t._v("即可;无需关注底层使用的何种日志框架。")])])])]),t._v(" "),n("h3",{attrs:{id:"覆盖测试"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#覆盖测试"}},[t._v("#")]),t._v(" 覆盖测试")]),t._v(" "),n("p",[t._v("对gone的代码编写了大量的单元测试代码,覆盖率达到90%。")]),t._v(" "),n("p",[n("a",{attrs:{href:"https://app.codecov.io/gh/gone-io/gone",target:"_blank",rel:"noopener noreferrer"}},[n("img",{attrs:{src:s(302),alt:"test"}}),n("OutboundLink")],1)]),t._v(" "),n("h2",{attrs:{id:"后续计划"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#后续计划"}},[t._v("#")]),t._v(" 后续计划")]),t._v(" "),n("ol",[n("li",[t._v("提供更多的开箱可用的Goners,接入更多的优秀的Web中间件。")]),t._v(" "),n("li",[t._v("编写一个完善的参考案例")])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/29.dbd850a7.js b/assets/js/28.bc8f695f.js similarity index 99% rename from assets/js/29.dbd850a7.js rename to assets/js/28.bc8f695f.js index b2ead17..2045d09 100644 --- a/assets/js/29.dbd850a7.js +++ b/assets/js/28.bc8f695f.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[29],{313:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"introduction"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#introduction"}},[t._v("#")]),t._v(" Introduction")]),t._v(" "),s("h2",{attrs:{id:"what-is-gone"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#what-is-gone"}},[t._v("#")]),t._v(" What is Gone?")]),t._v(" "),s("p",[t._v("Firstly, "),s("strong",[t._v("Gone")]),t._v(" is a lightweight "),s("strong",[t._v("dependency injection framework")]),t._v(" based on "),s("strong",[t._v("Golang")]),t._v(", inspired by Java's Spring Framework. Secondly, the "),s("strong",[t._v("Gone")]),t._v(" framework includes a series of built-in components that provide a comprehensive web development solution, offering capabilities commonly used in microservices such as service configuration, logging, service invocation, database access, and message middleware.")]),t._v(" "),s("p",[t._v("Let's use "),s("strong",[t._v("Gone")]),t._v(" to write a web service!")]),t._v(" "),s("h2",{attrs:{id:"web-service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-service"}},[t._v("#")]),t._v(" Web Service")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Implement a Goner. What is a Goner? => https://goner.fun/en/guide/core-concept.html#goner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, anonymously embedded, making the struct a Goner")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject root router")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Implement the Mount method to mount routes. The framework will automatically execute this method.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Define request structure")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tMsg "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"msg"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Register handler for `POST /hello`")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tto "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject http request query parameter `to`")]),t._v("\n\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject http request body")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to %s msg is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("to"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Msg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Start the service")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Invoke the framework's built-in component to load the gin framework")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury a Goner of type controller into the cemetery")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// What does bury mean? => https://goner.fun/en/guide/core-concept.html#bury")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// What is a cemetery? => https://goner.fun/en/guide/core-concept.html#cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Run the above code with: "),s("code",[t._v("go run main.go")]),t._v(". The program will listen on port "),s("code",[t._v("8080")]),t._v(". Test using curl:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("curl")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-X")]),t._v(" POST "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'http://localhost:8080/hello'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-H")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Content-Type: application/json'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n --data-raw "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('\'{"msg": "Hello there?"}\'')]),t._v("\n")])])]),s("p",[t._v("The result is:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"code"')]),t._v(":0,"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to msg is: Hello there?"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"concepts"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#concepts"}},[t._v("#")]),t._v(" Concepts")]),t._v(" "),s("blockquote",[s("p",[t._v("The code we write is ultimately lifeless unless it is run.")])]),t._v(" "),s("p",[t._v("In Gone, components are abstracted as "),s("strong",[t._v("Goner")]),t._v(". A "),s("strong",[t._v("Goner")]),t._v(" can have other "),s("strong",[t._v("Goners")]),t._v(" injected into it. Before starting Gone, all "),s("strong",[t._v("Goners")]),t._v(" need to be "),s("strong",[t._v("buried")]),t._v(" in the "),s("strong",[t._v("cemetery")]),t._v(". After starting Gone, all "),s("strong",[t._v("Goners")]),t._v(" will be "),s("strong",[t._v("resurrected")]),t._v(", forming a "),s("strong",[t._v("Heaven")]),t._v(' where "everyone is complete, and what they desire will be fulfilled".')]),t._v(" "),s("p",[t._v("To learn more, please read "),s("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone's core concepts"),s("OutboundLink")],1),t._v(".")])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[28],{314:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"introduction"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#introduction"}},[t._v("#")]),t._v(" Introduction")]),t._v(" "),s("h2",{attrs:{id:"what-is-gone"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#what-is-gone"}},[t._v("#")]),t._v(" What is Gone?")]),t._v(" "),s("p",[t._v("Firstly, "),s("strong",[t._v("Gone")]),t._v(" is a lightweight "),s("strong",[t._v("dependency injection framework")]),t._v(" based on "),s("strong",[t._v("Golang")]),t._v(", inspired by Java's Spring Framework. Secondly, the "),s("strong",[t._v("Gone")]),t._v(" framework includes a series of built-in components that provide a comprehensive web development solution, offering capabilities commonly used in microservices such as service configuration, logging, service invocation, database access, and message middleware.")]),t._v(" "),s("p",[t._v("Let's use "),s("strong",[t._v("Gone")]),t._v(" to write a web service!")]),t._v(" "),s("h2",{attrs:{id:"web-service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-service"}},[t._v("#")]),t._v(" Web Service")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Implement a Goner. What is a Goner? => https://goner.fun/en/guide/core-concept.html#goner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner flag, anonymously embedded, making the struct a Goner")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject root router")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Implement the Mount method to mount routes. The framework will automatically execute this method.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Define request structure")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tMsg "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"msg"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Register handler for `POST /hello`")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tto "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject http request query parameter `to`")]),t._v("\n\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject http request body")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to %s msg is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("to"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Msg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Start the service")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Invoke the framework's built-in component to load the gin framework")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury a Goner of type controller into the cemetery")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// What does bury mean? => https://goner.fun/en/guide/core-concept.html#bury")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// What is a cemetery? => https://goner.fun/en/guide/core-concept.html#cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Run the above code with: "),s("code",[t._v("go run main.go")]),t._v(". The program will listen on port "),s("code",[t._v("8080")]),t._v(". Test using curl:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("curl")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-X")]),t._v(" POST "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'http://localhost:8080/hello'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-H")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Content-Type: application/json'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n --data-raw "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('\'{"msg": "Hello there?"}\'')]),t._v("\n")])])]),s("p",[t._v("The result is:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"code"')]),t._v(":0,"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to msg is: Hello there?"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"concepts"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#concepts"}},[t._v("#")]),t._v(" Concepts")]),t._v(" "),s("blockquote",[s("p",[t._v("The code we write is ultimately lifeless unless it is run.")])]),t._v(" "),s("p",[t._v("In Gone, components are abstracted as "),s("strong",[t._v("Goner")]),t._v(". A "),s("strong",[t._v("Goner")]),t._v(" can have other "),s("strong",[t._v("Goners")]),t._v(" injected into it. Before starting Gone, all "),s("strong",[t._v("Goners")]),t._v(" need to be "),s("strong",[t._v("buried")]),t._v(" in the "),s("strong",[t._v("cemetery")]),t._v(". After starting Gone, all "),s("strong",[t._v("Goners")]),t._v(" will be "),s("strong",[t._v("resurrected")]),t._v(", forming a "),s("strong",[t._v("Heaven")]),t._v(' where "everyone is complete, and what they desire will be fulfilled".')]),t._v(" "),s("p",[t._v("To learn more, please read "),s("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone's core concepts"),s("OutboundLink")],1),t._v(".")])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/30.e22afe79.js b/assets/js/29.2e57d90a.js similarity index 98% rename from assets/js/30.e22afe79.js rename to assets/js/29.2e57d90a.js index 6c5be2e..0f8d6ba 100644 --- a/assets/js/30.e22afe79.js +++ b/assets/js/29.2e57d90a.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[30],{317:function(e,t,o){"use strict";o.r(t);var r=o(10),n=Object(r.a)({},(function(){var e=this,t=e._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#goners"}},[e._v("#")]),e._v(" Goners")]),e._v(" "),t("h2",{attrs:{id:"core-level-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#core-level-goners"}},[e._v("#")]),e._v(" Core-Level Goners")]),e._v(" "),t("blockquote",[t("p",[e._v("Core components are also Goners that can be injected into other Goners. These Goners are essential for the operation of Gone and are indispensable parts of the framework.")])]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("Interface")]),e._v(" "),t("th",[e._v("Implementation")]),e._v(" "),t("th",[e._v("GoneId")]),e._v(" "),t("th",[e._v("Description")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Heaven",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/heaven.go#L65",target:"_blank",rel:"noopener noreferrer"}},[e._v("heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-heaven")]),e._v(" "),t("td",[e._v("Responsible for resurrecting Goners from the cemetery, executing installed Hook functions, and managing the framework's startup and shutdown processes and states.")])]),e._v(" "),t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Cemetery",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/cemetery.go#L17",target:"_blank",rel:"noopener noreferrer"}},[e._v("cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-cemetery")]),e._v(" "),t("td",[e._v("Manages Goners and provides the Bury method for burying Goners. The dependency injection logic is mainly implemented in this structure.")])])])]),e._v(" "),t("h2",{attrs:{id:"built-in-framework-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#built-in-framework-goners"}},[e._v("#")]),e._v(" Built-in Framework Goners")]),e._v(" "),t("p",[e._v("Built-in framework Goners are developed to enrich the functionality of Gone, such as supporting web development, database connections, Redis, etc. The code is implemented in the "),t("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.4/goner",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner directory"),t("OutboundLink")],1),e._v(". Each subdirectory under this directory implements a feature of Gone, each containing the definition and implementation of one or more Goners.")]),e._v(" "),t("p",[e._v("For ease of use, we have defined the "),t("code",[e._v("Priest")]),e._v(" function in "),t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/v0.1.4/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner/priest.go"),t("OutboundLink")],1),e._v(", which can directly bury related Goners in batches according to functionality.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("Directory/Component")]),e._v(" "),t("th",[e._v("Functionality")]),e._v(" "),t("th",[e._v("Documentation")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[e._v("config")]),e._v(" "),t("td",[e._v("Reads configuration files from the config directory and allows configuration items to be injected into Goners")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/config.html"}},[e._v("Supporting configuration files with built-in Goners")])],1)]),e._v(" "),t("tr",[t("td",[e._v("logrus")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/sirupsen/logrus")]),e._v(" to provide logging methods and supports formatted log printing")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/logrus.html"}},[e._v("Logging")])],1)]),e._v(" "),t("tr",[t("td",[e._v("tracer")]),e._v(" "),t("td",[e._v("Log tracing, providing traceId; when handling the same request, logs can have the same traceId")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/tracer.html"}},[e._v("Tracking logs with traceId")])],1)]),e._v(" "),t("tr",[t("td",[e._v("gin")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/gin-gonic/gin")]),e._v(" to support web development with Gone")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("xorm")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("xorm.io/xorm")]),e._v(" for database operations")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/xorm.html"}},[e._v("Supporting database connections with built-in Goners")])],1)]),e._v(" "),t("tr",[t("td",[e._v("redis")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/gomodule/redigo/redis")]),e._v(" for Redis operations, providing Redis caching and distributed locking functionality")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/redis.html"}},[e._v("Using Redis for Distributed Locking and Caching")])],1)]),e._v(" "),t("tr",[t("td",[e._v("schedule")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/robfig/cron/v3")]),e._v(" to provide scheduling capabilities")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("urllib")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/imroc/req/v3")]),e._v(" to provide HTTP call capabilities")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("grpc")]),e._v(" "),t("td",[e._v("Provides capabilities for developing gRPC servers and clients")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("cmux")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/soheilhy/cmux")]),e._v(" to enable mixed services on a unified port")]),e._v(" "),t("td",[e._v("-")])])])]),e._v(" "),t("h2",{attrs:{id:"ecosystem-level-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#ecosystem-level-goners"}},[e._v("#")]),e._v(" Ecosystem-Level Goners")]),e._v(" "),t("ul",[t("li",[t("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[e._v("emitter"),t("OutboundLink")],1),e._v(", wraps event handling and can be used for event storming in DDD")])])])}),[],!1,null,null,null);t.default=n.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[29],{315:function(e,t,o){"use strict";o.r(t);var r=o(10),n=Object(r.a)({},(function(){var e=this,t=e._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#goners"}},[e._v("#")]),e._v(" Goners")]),e._v(" "),t("h2",{attrs:{id:"core-level-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#core-level-goners"}},[e._v("#")]),e._v(" Core-Level Goners")]),e._v(" "),t("blockquote",[t("p",[e._v("Core components are also Goners that can be injected into other Goners. These Goners are essential for the operation of Gone and are indispensable parts of the framework.")])]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("Interface")]),e._v(" "),t("th",[e._v("Implementation")]),e._v(" "),t("th",[e._v("GoneId")]),e._v(" "),t("th",[e._v("Description")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Heaven",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/heaven.go#L65",target:"_blank",rel:"noopener noreferrer"}},[e._v("heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-heaven")]),e._v(" "),t("td",[e._v("Responsible for resurrecting Goners from the cemetery, executing installed Hook functions, and managing the framework's startup and shutdown processes and states.")])]),e._v(" "),t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Cemetery",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/cemetery.go#L17",target:"_blank",rel:"noopener noreferrer"}},[e._v("cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-cemetery")]),e._v(" "),t("td",[e._v("Manages Goners and provides the Bury method for burying Goners. The dependency injection logic is mainly implemented in this structure.")])])])]),e._v(" "),t("h2",{attrs:{id:"built-in-framework-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#built-in-framework-goners"}},[e._v("#")]),e._v(" Built-in Framework Goners")]),e._v(" "),t("p",[e._v("Built-in framework Goners are developed to enrich the functionality of Gone, such as supporting web development, database connections, Redis, etc. The code is implemented in the "),t("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.4/goner",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner directory"),t("OutboundLink")],1),e._v(". Each subdirectory under this directory implements a feature of Gone, each containing the definition and implementation of one or more Goners.")]),e._v(" "),t("p",[e._v("For ease of use, we have defined the "),t("code",[e._v("Priest")]),e._v(" function in "),t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/v0.1.4/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner/priest.go"),t("OutboundLink")],1),e._v(", which can directly bury related Goners in batches according to functionality.")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("Directory/Component")]),e._v(" "),t("th",[e._v("Functionality")]),e._v(" "),t("th",[e._v("Documentation")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[e._v("config")]),e._v(" "),t("td",[e._v("Reads configuration files from the config directory and allows configuration items to be injected into Goners")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/config.html"}},[e._v("Supporting configuration files with built-in Goners")])],1)]),e._v(" "),t("tr",[t("td",[e._v("logrus")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/sirupsen/logrus")]),e._v(" to provide logging methods and supports formatted log printing")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/logrus.html"}},[e._v("Logging")])],1)]),e._v(" "),t("tr",[t("td",[e._v("tracer")]),e._v(" "),t("td",[e._v("Log tracing, providing traceId; when handling the same request, logs can have the same traceId")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/tracer.html"}},[e._v("Tracking logs with traceId")])],1)]),e._v(" "),t("tr",[t("td",[e._v("gin")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/gin-gonic/gin")]),e._v(" to support web development with Gone")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("xorm")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("xorm.io/xorm")]),e._v(" for database operations")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/xorm.html"}},[e._v("Supporting database connections with built-in Goners")])],1)]),e._v(" "),t("tr",[t("td",[e._v("redis")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/gomodule/redigo/redis")]),e._v(" for Redis operations, providing Redis caching and distributed locking functionality")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/guide/redis.html"}},[e._v("Using Redis for Distributed Locking and Caching")])],1)]),e._v(" "),t("tr",[t("td",[e._v("schedule")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/robfig/cron/v3")]),e._v(" to provide scheduling capabilities")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("urllib")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/imroc/req/v3")]),e._v(" to provide HTTP call capabilities")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("grpc")]),e._v(" "),t("td",[e._v("Provides capabilities for developing gRPC servers and clients")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("cmux")]),e._v(" "),t("td",[e._v("Wraps "),t("code",[e._v("github.com/soheilhy/cmux")]),e._v(" to enable mixed services on a unified port")]),e._v(" "),t("td",[e._v("-")])])])]),e._v(" "),t("h2",{attrs:{id:"ecosystem-level-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#ecosystem-level-goners"}},[e._v("#")]),e._v(" Ecosystem-Level Goners")]),e._v(" "),t("ul",[t("li",[t("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[e._v("emitter"),t("OutboundLink")],1),e._v(", wraps event handling and can be used for event storming in DDD")])])])}),[],!1,null,null,null);t.default=n.exports}}]); \ No newline at end of file diff --git a/assets/js/3.c0437673.js b/assets/js/3.d926fa22.js similarity index 94% rename from assets/js/3.c0437673.js rename to assets/js/3.d926fa22.js index e4cb25c..4526ea2 100644 --- a/assets/js/3.c0437673.js +++ b/assets/js/3.d926fa22.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[3,18,21],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return r})),n.d(e,"a",(function(){return s})),n.d(e,"i",(function(){return a})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return d})),n.d(e,"k",(function(){return h})),n.d(e,"l",(function(){return f})),n.d(e,"c",(function(){return g})),n.d(e,"j",(function(){return m}));n(91);const r=/#.*$/,i=/\.(md|html)$/,s=/\/$/,a=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(r,"").replace(i,"")}function l(t){return a.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function p(t){if(l(t))return t;const e=t.match(r),n=e?e[0]:"",i=o(t);return s.test(i)?t:i+".html"+n}function d(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(r);if(e)return e[0]}(e);if(i&&n!==i)return!1;return o(t.path)===o(e)}function h(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const s=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,r,i=1){if("string"==typeof e)return h(n,e,r);if(Array.isArray(e))return Object.assign(h(n,e[0],r),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(h(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function b(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},245:function(t,e,n){"use strict";n.r(e);var r={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},i=(n(246),n(10)),s=Object(i.a)(r,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},248:function(t,e,n){},253:function(t,e,n){},255:function(t,e,n){"use strict";n(248)},258:function(t,e,n){"use strict";n.r(e);var r=n(271),i=n(260),s=n(242);function a(t,e){if("group"===e.type){const n=e.path&&Object(s.e)(t,e.path),r=e.children.some(e=>"group"===e.type?a(t,e):"page"===e.type&&Object(s.e)(t,e.path));return n||r}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:r.default,SidebarLink:i.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let n=0;n-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(s.e)(this.$route,t.regularPath)}}},l=n(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(n,r){return e("li",{key:r},["group"===n.type?e("SidebarGroup",{attrs:{item:n,open:r===t.openGroupIndex,collapsable:n.collapsable||n.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(r)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:n}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},260:function(t,e,n){"use strict";n.r(e);var r=n(242);function i(t,e,n,r,i){const s={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:r,"sidebar-link":!0}};return i>2&&(s.style={"padding-left":i+"rem"}),t("RouterLink",s,n)}function s(t,e,n,a,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(r.e)(a,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[i(t,n+"#"+e.slug,e.title,u,e.level-1),s(t,e.children,n,a,o,l+1)])}))}var a={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:a,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(r.e)(a,u.path),d="auto"===u.type?p||u.children.some(t=>Object(r.e)(a,u.basePath+"#"+t.slug)):p,h="external"===u.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,u.path,u.title||u.path):i(t,u.path,u.title||u.path,d),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[h,s(t,u.children,u.basePath,a,f)];if((d||b)&&u.headers&&!r.d.test(u.path)){return[h,s(t,Object(r.c)(u.headers),u.path,a,f)]}return h}},o=(n(255),n(10)),l=Object(o.a)(a,void 0,void 0,!1,null,null,null);e.default=l.exports},268:function(t,e,n){"use strict";n(253)},271:function(t,e,n){"use strict";n.r(e);var r=n(242),i={name:"SidebarGroup",components:{DropdownTransition:n(245).default},props:["item","open","collapsable","depth"],beforeCreate(){this.$options.components.SidebarLinks=n(258).default},methods:{isActive:r.e}},s=(n(268),n(10)),a=Object(s.a)(i,(function(){var t=this,e=t._self._c;return e("section",{staticClass:"sidebar-group",class:[{collapsable:t.collapsable,"is-sub-group":0!==t.depth},"depth-"+t.depth]},[t.item.path?e("RouterLink",{staticClass:"sidebar-heading clickable",class:{open:t.open,active:t.isActive(t.$route,t.item.path)},attrs:{to:t.item.path},nativeOn:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]):e("p",{staticClass:"sidebar-heading",class:{open:t.open},on:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]),t._v(" "),e("DropdownTransition",[t.open||!t.collapsable?e("SidebarLinks",{staticClass:"sidebar-group-items",attrs:{items:t.item.children,"sidebar-depth":t.item.sidebarDepth,"initial-open-group-index":t.item.initialOpenGroupIndex,depth:t.depth+1}}):t._e()],1)],1)}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[3,18,21],{242:function(t,e,n){"use strict";n.d(e,"d",(function(){return r})),n.d(e,"a",(function(){return s})),n.d(e,"i",(function(){return a})),n.d(e,"f",(function(){return l})),n.d(e,"g",(function(){return u})),n.d(e,"h",(function(){return c})),n.d(e,"b",(function(){return p})),n.d(e,"e",(function(){return d})),n.d(e,"k",(function(){return h})),n.d(e,"l",(function(){return f})),n.d(e,"c",(function(){return g})),n.d(e,"j",(function(){return m}));n(91);const r=/#.*$/,i=/\.(md|html)$/,s=/\/$/,a=/^[a-z]+:/i;function o(t){return decodeURI(t).replace(r,"").replace(i,"")}function l(t){return a.test(t)}function u(t){return/^mailto:/.test(t)}function c(t){return/^tel:/.test(t)}function p(t){if(l(t))return t;const e=t.match(r),n=e?e[0]:"",i=o(t);return s.test(i)?t:i+".html"+n}function d(t,e){const n=decodeURIComponent(t.hash),i=function(t){const e=t.match(r);if(e)return e[0]}(e);if(i&&n!==i)return!1;return o(t.path)===o(e)}function h(t,e,n){if(l(e))return{type:"external",path:e};n&&(e=function(t,e,n){const r=t.charAt(0);if("/"===r)return t;if("?"===r||"#"===r)return e+t;const i=e.split("/");n&&i[i.length-1]||i.pop();const s=t.replace(/^\//,"").split("/");for(let t=0;tfunction t(e,n,r,i=1){if("string"==typeof e)return h(n,e,r);if(Array.isArray(e))return Object.assign(h(n,e[0],r),{title:e[1]});{const s=e.children||[];return 0===s.length&&e.path?Object.assign(h(n,e.path,r),{title:e.title}):{type:"group",path:e.path,title:e.title,sidebarDepth:e.sidebarDepth,initialOpenGroupIndex:e.initialOpenGroupIndex,children:s.map(e=>t(e,n,r,i+1)),collapsable:!1!==e.collapsable}}}(t,i,n)):[]}return[]}function b(t){const e=g(t.headers||[]);return[{type:"group",collapsable:!1,title:t.title,path:null,children:e.map(e=>({type:"auto",title:e.title,basePath:t.path,path:t.path+"#"+e.slug,children:e.children||[]}))}]}function g(t){let e;return(t=t.map(t=>Object.assign({},t))).forEach(t=>{2===t.level?e=t:e&&(e.children||(e.children=[])).push(t)}),t.filter(t=>2===t.level)}function m(t){return Object.assign(t,{type:t.items&&t.items.length?"links":"link"})}},243:function(t,e,n){},245:function(t,e,n){"use strict";n.r(e);var r={name:"DropdownTransition",methods:{setHeight(t){t.style.height=t.scrollHeight+"px"},unsetHeight(t){t.style.height=""}}},i=(n(246),n(10)),s=Object(i.a)(r,(function(){return(0,this._self._c)("transition",{attrs:{name:"dropdown"},on:{enter:this.setHeight,"after-enter":this.unsetHeight,"before-leave":this.setHeight}},[this._t("default")],2)}),[],!1,null,null,null);e.default=s.exports},246:function(t,e,n){"use strict";n(243)},248:function(t,e,n){},253:function(t,e,n){},255:function(t,e,n){"use strict";n(248)},256:function(t,e,n){"use strict";n.r(e);var r=n(271),i=n(258),s=n(242);function a(t,e){if("group"===e.type){const n=e.path&&Object(s.e)(t,e.path),r=e.children.some(e=>"group"===e.type?a(t,e):"page"===e.type&&Object(s.e)(t,e.path));return n||r}return!1}var o={name:"SidebarLinks",components:{SidebarGroup:r.default,SidebarLink:i.default},props:["items","depth","sidebarDepth","initialOpenGroupIndex"],data(){return{openGroupIndex:this.initialOpenGroupIndex||0}},watch:{$route(){this.refreshIndex()}},created(){this.refreshIndex()},methods:{refreshIndex(){const t=function(t,e){for(let n=0;n-1&&(this.openGroupIndex=t)},toggleGroup(t){this.openGroupIndex=t===this.openGroupIndex?-1:t},isActive(t){return Object(s.e)(this.$route,t.regularPath)}}},l=n(10),u=Object(l.a)(o,(function(){var t=this,e=t._self._c;return t.items.length?e("ul",{staticClass:"sidebar-links"},t._l(t.items,(function(n,r){return e("li",{key:r},["group"===n.type?e("SidebarGroup",{attrs:{item:n,open:r===t.openGroupIndex,collapsable:n.collapsable||n.collapsible,depth:t.depth},on:{toggle:function(e){return t.toggleGroup(r)}}}):e("SidebarLink",{attrs:{"sidebar-depth":t.sidebarDepth,item:n}})],1)})),0):t._e()}),[],!1,null,null,null);e.default=u.exports},258:function(t,e,n){"use strict";n.r(e);var r=n(242);function i(t,e,n,r,i){const s={props:{to:e,activeClass:"",exactActiveClass:""},class:{active:r,"sidebar-link":!0}};return i>2&&(s.style={"padding-left":i+"rem"}),t("RouterLink",s,n)}function s(t,e,n,a,o,l=1){return!e||l>o?null:t("ul",{class:"sidebar-sub-headers"},e.map(e=>{const u=Object(r.e)(a,n+"#"+e.slug);return t("li",{class:"sidebar-sub-header"},[i(t,n+"#"+e.slug,e.title,u,e.level-1),s(t,e.children,n,a,o,l+1)])}))}var a={functional:!0,props:["item","sidebarDepth"],render(t,{parent:{$page:e,$site:n,$route:a,$themeConfig:o,$themeLocaleConfig:l},props:{item:u,sidebarDepth:c}}){const p=Object(r.e)(a,u.path),d="auto"===u.type?p||u.children.some(t=>Object(r.e)(a,u.basePath+"#"+t.slug)):p,h="external"===u.type?function(t,e,n){return t("a",{attrs:{href:e,target:"_blank",rel:"noopener noreferrer"},class:{"sidebar-link":!0}},[n,t("OutboundLink")])}(t,u.path,u.title||u.path):i(t,u.path,u.title||u.path,d),f=[e.frontmatter.sidebarDepth,c,l.sidebarDepth,o.sidebarDepth,1].find(t=>void 0!==t),b=l.displayAllHeaders||o.displayAllHeaders;if("auto"===u.type)return[h,s(t,u.children,u.basePath,a,f)];if((d||b)&&u.headers&&!r.d.test(u.path)){return[h,s(t,Object(r.c)(u.headers),u.path,a,f)]}return h}},o=(n(255),n(10)),l=Object(o.a)(a,void 0,void 0,!1,null,null,null);e.default=l.exports},266:function(t,e,n){"use strict";n(253)},271:function(t,e,n){"use strict";n.r(e);var r=n(242),i={name:"SidebarGroup",components:{DropdownTransition:n(245).default},props:["item","open","collapsable","depth"],beforeCreate(){this.$options.components.SidebarLinks=n(256).default},methods:{isActive:r.e}},s=(n(266),n(10)),a=Object(s.a)(i,(function(){var t=this,e=t._self._c;return e("section",{staticClass:"sidebar-group",class:[{collapsable:t.collapsable,"is-sub-group":0!==t.depth},"depth-"+t.depth]},[t.item.path?e("RouterLink",{staticClass:"sidebar-heading clickable",class:{open:t.open,active:t.isActive(t.$route,t.item.path)},attrs:{to:t.item.path},nativeOn:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]):e("p",{staticClass:"sidebar-heading",class:{open:t.open},on:{click:function(e){return t.$emit("toggle")}}},[e("span",[t._v(t._s(t.item.title))]),t._v(" "),t.collapsable?e("span",{staticClass:"arrow",class:t.open?"down":"right"}):t._e()]),t._v(" "),e("DropdownTransition",[t.open||!t.collapsable?e("SidebarLinks",{staticClass:"sidebar-group-items",attrs:{items:t.item.children,"sidebar-depth":t.item.sidebarDepth,"initial-open-group-index":t.item.initialOpenGroupIndex,depth:t.depth+1}}):t._e()],1)],1)}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/31.aab0a190.js b/assets/js/30.339618b0.js similarity index 97% rename from assets/js/31.aab0a190.js rename to assets/js/30.339618b0.js index b2301a6..3f5dc5b 100644 --- a/assets/js/31.aab0a190.js +++ b/assets/js/30.339618b0.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[31],{314:function(t,e,o){"use strict";o.r(e);var r=o(10),a=Object(r.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"development-guide"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#development-guide"}},[t._v("#")]),t._v(" Development Guide")]),t._v(" "),e("h2",{attrs:{id:"core-concepts-of-gone"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#core-concepts-of-gone"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/core-concept.html"}},[t._v("Core Concepts of Gone")])],1),t._v(" "),e("h2",{attrs:{id:"what-injection-methods-does-gone-support"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#what-injection-methods-does-gone-support"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/goner-inject.html"}},[t._v("What Injection Methods Does Gone Support?")])],1),t._v(" "),e("h2",{attrs:{id:"how-to-gracefully-use-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#how-to-gracefully-use-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/use-inner-goner.html"}},[t._v("How to Gracefully Use Built-in Goners?")])],1),t._v(" "),e("h2",{attrs:{id:"supporting-configuration-files-via-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supporting-configuration-files-via-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/config.html"}},[t._v("Supporting Configuration Files via Built-in Goners")])],1),t._v(" "),e("h2",{attrs:{id:"supporting-database-connections-via-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supporting-database-connections-via-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/xorm.html"}},[t._v("Supporting Database Connections via Built-in Goners")])],1),t._v(" "),e("h2",{attrs:{id:"hook-functions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#hook-functions"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/hooks.html"}},[t._v("Hook Functions")])],1),t._v(" "),e("h2",{attrs:{id:"logging-output"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#logging-output"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/logrus.html"}},[t._v("Logging Output")])],1),t._v(" "),e("h2",{attrs:{id:"tracing-logs-with-traceid"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#tracing-logs-with-traceid"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/tracer.html"}},[t._v("Tracing Logs with TraceID")])],1),t._v(" "),e("h2",{attrs:{id:"auto-generating-priests"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#auto-generating-priests"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/auto-gen-priest.html"}},[t._v("Auto-generating Priests")])],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[30],{316:function(t,e,o){"use strict";o.r(e);var r=o(10),a=Object(r.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"development-guide"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#development-guide"}},[t._v("#")]),t._v(" Development Guide")]),t._v(" "),e("h2",{attrs:{id:"core-concepts-of-gone"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#core-concepts-of-gone"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/core-concept.html"}},[t._v("Core Concepts of Gone")])],1),t._v(" "),e("h2",{attrs:{id:"what-injection-methods-does-gone-support"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#what-injection-methods-does-gone-support"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/goner-inject.html"}},[t._v("What Injection Methods Does Gone Support?")])],1),t._v(" "),e("h2",{attrs:{id:"how-to-gracefully-use-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#how-to-gracefully-use-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/use-inner-goner.html"}},[t._v("How to Gracefully Use Built-in Goners?")])],1),t._v(" "),e("h2",{attrs:{id:"supporting-configuration-files-via-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supporting-configuration-files-via-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/config.html"}},[t._v("Supporting Configuration Files via Built-in Goners")])],1),t._v(" "),e("h2",{attrs:{id:"supporting-database-connections-via-built-in-goners"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supporting-database-connections-via-built-in-goners"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/xorm.html"}},[t._v("Supporting Database Connections via Built-in Goners")])],1),t._v(" "),e("h2",{attrs:{id:"hook-functions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#hook-functions"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/hooks.html"}},[t._v("Hook Functions")])],1),t._v(" "),e("h2",{attrs:{id:"logging-output"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#logging-output"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/logrus.html"}},[t._v("Logging Output")])],1),t._v(" "),e("h2",{attrs:{id:"tracing-logs-with-traceid"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#tracing-logs-with-traceid"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/tracer.html"}},[t._v("Tracing Logs with TraceID")])],1),t._v(" "),e("h2",{attrs:{id:"auto-generating-priests"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#auto-generating-priests"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/auto-gen-priest.html"}},[t._v("Auto-generating Priests")])],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/32.c6d9753d.js b/assets/js/31.96cd098f.js similarity index 99% rename from assets/js/32.c6d9753d.js rename to assets/js/31.96cd098f.js index 6452188..e041f1e 100644 --- a/assets/js/32.c6d9753d.js +++ b/assets/js/31.96cd098f.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[32],{315:function(t,e,a){"use strict";a.r(e);var n=a(10),s=Object(n.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"auto-generate-priest"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#auto-generate-priest"}},[t._v("#")]),t._v(" Auto-generate Priest")]),t._v(" "),e("p"),e("div",{staticClass:"table-of-contents"},[e("ul",[e("li",[e("a",{attrs:{href:"#getting-started"}},[t._v("Getting Started")]),e("ul",[e("li",[e("a",{attrs:{href:"#_1-install-the-gone-auxiliary-tool"}},[t._v("1. Install the gone auxiliary tool")])]),e("li",[e("a",{attrs:{href:"#_2-write-the-goner"}},[t._v("2. Write the Goner")])]),e("li",[e("a",{attrs:{href:"#_3-generate-code"}},[t._v("3. Generate code")])])])]),e("li",[e("a",{attrs:{href:"#best-practices"}},[t._v("Best Practices")])])])]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/core-concept.html"}},[t._v("What is Priest?")]),e("p"),t._v(" "),e("h2",{attrs:{id:"getting-started"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#getting-started"}},[t._v("#")]),t._v(" Getting Started")]),t._v(" "),e("p",[t._v("The "),e("code",[t._v("gone priest")]),t._v(" command scans for special comments in the code "),e("code",[t._v("//go:gone")]),t._v(" to generate Priest functions. Currently, "),e("code",[t._v("//go:gone")]),t._v(" is only used to mark functions in the form of "),e("code",[t._v("func() gone.Goner")]),t._v(" and "),e("code",[t._v("func() (gone.Goner, gone.GonerId)")]),t._v(". The functions must be exported (starting with a capital letter).")]),t._v(" "),e("h3",{attrs:{id:"_1-install-the-gone-auxiliary-tool"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_1-install-the-gone-auxiliary-tool"}},[t._v("#")]),t._v(" 1. Install the gone auxiliary tool")]),t._v(" "),e("p",[t._v("Run the following command:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[t._v("go "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),e("p",[t._v("For more information on gone, refer to the "),e("RouterLink",{attrs:{to:"/references/gone-tool.html"}},[t._v("gone auxiliary tools")]),t._v(" documentation.")],1),t._v(" "),e("h3",{attrs:{id:"_2-write-the-goner"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_2-write-the-goner"}},[t._v("#")]),t._v(" 2. Write the Goner")]),t._v(" "),e("p",[t._v("Create a project and a file:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[e("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" demo\n"),e("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" demo\ngo mod init demo\n"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" demo.go\n")])])]),e("p",[t._v("Edit the demo.go file with the following code:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Demo"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"_3-generate-code"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_3-generate-code"}},[t._v("#")]),t._v(" 3. Generate code")]),t._v(" "),e("p",[t._v("Execute:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[t._v("gone priest "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" ./ "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" demo\n")])])]),e("p",[t._v("This will generate the file priest.go with the following content:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"best-practices"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#best-practices"}},[t._v("#")]),t._v(" Best Practices")]),t._v(" "),e("ul",[e("li",[t._v("Create a MasterPriest function for the Gone framework startup.")]),t._v(" "),e("li",[t._v("Call the Priest functions of project dependency packages in the MasterPriest function.")]),t._v(" "),e("li",[t._v('Place the "gone priest" command after the '),e("code",[t._v("//go:generate")]),t._v(" comment in the MasterPriest function, for example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" internal\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:generate gone priest -s . -p $GOPACKAGE -f Priest -o priest.go")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call the Priest function of project dependency packages")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call the generated Priest function")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("blockquote",[e("p",[t._v("tips: Running "),e("code",[t._v("go generate ./...")]),t._v(" executes all commands following "),e("code",[t._v("//go:generate")]),t._v(" comments.")])]),t._v(" "),e("ul",[e("li",[t._v("Call the generated Priest function in the MasterPriest function.")]),t._v(" "),e("li",[t._v("Ignore the generated files in git by adding them to the "),e("code",[t._v(".gitignore")]),t._v(" file.")])]),t._v(" "),e("p",[t._v("Following this logic, we have created a reference Web blank project "),e("a",{attrs:{href:"https://github.com/gone-io/examples/tree/main/empty",target:"_blank",rel:"noopener noreferrer"}},[t._v("Demo"),e("OutboundLink")],1),t._v(", which has been pushed to GitHub. Click to open it now. Additionally, you can run "),e("code",[t._v("gone create web-app")]),t._v(" to generate a blank project, as explained in the "),e("a",{attrs:{href:"https://goner.fun/en/quick-start/",target:"_blank",rel:"noopener noreferrer"}},[t._v("Quick Start"),e("OutboundLink")],1),t._v(" guide.")])],1)}),[],!1,null,null,null);e.default=s.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[31],{313:function(t,e,a){"use strict";a.r(e);var n=a(10),s=Object(n.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"auto-generate-priest"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#auto-generate-priest"}},[t._v("#")]),t._v(" Auto-generate Priest")]),t._v(" "),e("p"),e("div",{staticClass:"table-of-contents"},[e("ul",[e("li",[e("a",{attrs:{href:"#getting-started"}},[t._v("Getting Started")]),e("ul",[e("li",[e("a",{attrs:{href:"#_1-install-the-gone-auxiliary-tool"}},[t._v("1. Install the gone auxiliary tool")])]),e("li",[e("a",{attrs:{href:"#_2-write-the-goner"}},[t._v("2. Write the Goner")])]),e("li",[e("a",{attrs:{href:"#_3-generate-code"}},[t._v("3. Generate code")])])])]),e("li",[e("a",{attrs:{href:"#best-practices"}},[t._v("Best Practices")])])])]),t._v(" "),e("RouterLink",{attrs:{to:"/guide/core-concept.html"}},[t._v("What is Priest?")]),e("p"),t._v(" "),e("h2",{attrs:{id:"getting-started"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#getting-started"}},[t._v("#")]),t._v(" Getting Started")]),t._v(" "),e("p",[t._v("The "),e("code",[t._v("gone priest")]),t._v(" command scans for special comments in the code "),e("code",[t._v("//go:gone")]),t._v(" to generate Priest functions. Currently, "),e("code",[t._v("//go:gone")]),t._v(" is only used to mark functions in the form of "),e("code",[t._v("func() gone.Goner")]),t._v(" and "),e("code",[t._v("func() (gone.Goner, gone.GonerId)")]),t._v(". The functions must be exported (starting with a capital letter).")]),t._v(" "),e("h3",{attrs:{id:"_1-install-the-gone-auxiliary-tool"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_1-install-the-gone-auxiliary-tool"}},[t._v("#")]),t._v(" 1. Install the gone auxiliary tool")]),t._v(" "),e("p",[t._v("Run the following command:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[t._v("go "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),e("p",[t._v("For more information on gone, refer to the "),e("RouterLink",{attrs:{to:"/references/gone-tool.html"}},[t._v("gone auxiliary tools")]),t._v(" documentation.")],1),t._v(" "),e("h3",{attrs:{id:"_2-write-the-goner"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_2-write-the-goner"}},[t._v("#")]),t._v(" 2. Write the Goner")]),t._v(" "),e("p",[t._v("Create a project and a file:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[e("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" demo\n"),e("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" demo\ngo mod init demo\n"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" demo.go\n")])])]),e("p",[t._v("Edit the demo.go file with the following code:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Demo"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"_3-generate-code"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#_3-generate-code"}},[t._v("#")]),t._v(" 3. Generate code")]),t._v(" "),e("p",[t._v("Execute:")]),t._v(" "),e("div",{staticClass:"language-bash extra-class"},[e("pre",{pre:!0,attrs:{class:"language-bash"}},[e("code",[t._v("gone priest "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" ./ "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go "),e("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" demo\n")])])]),e("p",[t._v("This will generate the file priest.go with the following content:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"best-practices"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#best-practices"}},[t._v("#")]),t._v(" Best Practices")]),t._v(" "),e("ul",[e("li",[t._v("Create a MasterPriest function for the Gone framework startup.")]),t._v(" "),e("li",[t._v("Call the Priest functions of project dependency packages in the MasterPriest function.")]),t._v(" "),e("li",[t._v('Place the "gone priest" command after the '),e("code",[t._v("//go:generate")]),t._v(" comment in the MasterPriest function, for example:")])]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" internal\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:generate gone priest -s . -p $GOPACKAGE -f Priest -o priest.go")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call the Priest function of project dependency packages")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call the generated Priest function")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("blockquote",[e("p",[t._v("tips: Running "),e("code",[t._v("go generate ./...")]),t._v(" executes all commands following "),e("code",[t._v("//go:generate")]),t._v(" comments.")])]),t._v(" "),e("ul",[e("li",[t._v("Call the generated Priest function in the MasterPriest function.")]),t._v(" "),e("li",[t._v("Ignore the generated files in git by adding them to the "),e("code",[t._v(".gitignore")]),t._v(" file.")])]),t._v(" "),e("p",[t._v("Following this logic, we have created a reference Web blank project "),e("a",{attrs:{href:"https://github.com/gone-io/examples/tree/main/empty",target:"_blank",rel:"noopener noreferrer"}},[t._v("Demo"),e("OutboundLink")],1),t._v(", which has been pushed to GitHub. Click to open it now. Additionally, you can run "),e("code",[t._v("gone create web-app")]),t._v(" to generate a blank project, as explained in the "),e("a",{attrs:{href:"https://goner.fun/en/quick-start/",target:"_blank",rel:"noopener noreferrer"}},[t._v("Quick Start"),e("OutboundLink")],1),t._v(" guide.")])],1)}),[],!1,null,null,null);e.default=s.exports}}]); \ No newline at end of file diff --git a/assets/js/33.b1d76de0.js b/assets/js/32.b77c9ae9.js similarity index 99% rename from assets/js/33.b1d76de0.js rename to assets/js/32.b77c9ae9.js index 8b3c803..a17d293 100644 --- a/assets/js/33.b1d76de0.js +++ b/assets/js/32.b77c9ae9.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[33],{316:function(t,n,e){"use strict";e.r(n);var a=e(10),s=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"configuring-with-built-in-goners-support"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuring-with-built-in-goners-support"}},[t._v("#")]),t._v(" Configuring with Built-in Goners Support")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#example"}},[t._v("Example")]),n("ul",[n("li",[n("a",{attrs:{href:"#_1-create-a-mod"}},[t._v("1. Create a mod")])]),n("li",[n("a",{attrs:{href:"#_2-add-a-configuration-file"}},[t._v("2. Add a configuration file")])]),n("li",[n("a",{attrs:{href:"#_3-add-code"}},[t._v("3. Add code")])]),n("li",[n("a",{attrs:{href:"#_4-run"}},[t._v("4. Run")])])])]),n("li",[n("a",{attrs:{href:"#configuration-injection"}},[t._v("Configuration Injection")]),n("ul",[n("li",[n("a",{attrs:{href:"#tag-format-used"}},[t._v("Tag Format Used")])]),n("li",[n("a",{attrs:{href:"#supported-data-types-for-injection"}},[t._v("Supported Data Types for Injection")])]),n("li",[n("a",{attrs:{href:"#configuration-files"}},[t._v("Configuration Files")])])])])])]),n("p"),t._v(" "),n("p",[t._v("Gone provides a method to read configuration files using the built-in Goners. Currently, the configuration file format is only supported in "),n("code",[t._v(".properties")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"example"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#example"}},[t._v("#")]),t._v(" Example")]),t._v(" "),n("blockquote",[n("p",[t._v("You can find the source code of the example "),n("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-config",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"_1-create-a-mod"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-create-a-mod"}},[t._v("#")]),t._v(" 1. Create a mod")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go mod init use-config\n")])])]),n("h3",{attrs:{id:"_2-add-a-configuration-file"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-add-a-configuration-file"}},[t._v("#")]),t._v(" 2. Add a configuration file")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" config/default.properties\n")])])]),n("p",[t._v("The content of the "),n("code",[t._v("config/default.properties")]),t._v(" file is as follows:")]),t._v(" "),n("div",{staticClass:"language-properties extra-class"},[n("pre",{pre:!0,attrs:{class:"language-properties"}},[n("code",[n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int8")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("130")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10.222")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("config test")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.bool")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("true")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.duration")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10h")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("100x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("200y")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000y")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111y")]),t._v("\n")])])]),n("h3",{attrs:{id:"_3-add-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-add-code"}},[t._v("#")]),t._v(" 3. Add code")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" main.go\n")])])]),n("p",[t._v("The content of the "),n("code",[t._v("main.go")]),t._v(" file is as follows:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SubConf "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tX "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"x"`')]),t._v("\n\tY "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"y"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" UseConfig "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v("\n\tprintInt "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer to int")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.float64"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.string"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.bool"`')]),t._v("\n\tduration time"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.duration"`')]),t._v("\n\tdefaultV "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.default,default=ok"`')]),t._v("\n\n\tsub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SubConf "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.sub"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer to struct")]),t._v("\n\n\tsubs "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("SubConf "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.subs"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// array")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("UseConfig"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int8=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"printInt=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("printInt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"float64=%f\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string=%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"bool=%t\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"duration=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("duration"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"defaultV=%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("defaultV"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"sub.x=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("sub"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"subs=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("subs"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("UseConfig"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_4-run"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-run"}},[t._v("#")]),t._v(" 4. Run")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go mod tidy\ngo run main.go\n")])])]),n("p",[t._v("The output will be as follows:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("int=10\nint8=-126\nprintInt=130\nfloat64=10.222000\nstring=config test\nbool=true\nduration=10h0m0s\ndefaultV=ok\nsub.x=&{100x 200y}\nsubs=[{0000x 0000y} {1111x 1111y}]\n")])])]),n("h2",{attrs:{id:"configuration-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-injection"}},[t._v("#")]),t._v(" Configuration Injection")]),t._v(" "),n("p",[t._v('As seen from the example, configuration items can also be "dependency injected" into Goners.')]),t._v(" "),n("h3",{attrs:{id:"tag-format-used"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#tag-format-used"}},[t._v("#")]),t._v(" Tag Format Used")]),t._v(" "),n("p",[t._v("The tag format for configuration injection is as follows:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v('gone:"config,${key},default=${defaultValue}"\n')])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Explanation:")]),t._v(" "),n("ol",[n("li",[t._v("The tag name remains "),n("code",[t._v("gone")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v("config")]),t._v(" is a fixed value, indicating that the property is a configuration.")]),t._v(" "),n("li",[n("code",[t._v("${key}")]),t._v(" represents the key in the configuration file.")]),t._v(" "),n("li",[n("code",[t._v("default=${defaultValue}")]),t._v(" is used to specify a default value, which can be omitted; "),n("code",[t._v("${defaultValue}")]),t._v(" is the default value. If no default value is specified and the corresponding key is missing in the configuration file, an error will be raised.")])])]),t._v(" "),n("h3",{attrs:{id:"supported-data-types-for-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#supported-data-types-for-injection"}},[t._v("#")]),t._v(" Supported Data Types for Injection")]),t._v(" "),n("blockquote",[n("p",[t._v("In the "),n("a",{attrs:{href:"https://goner.fun/guide/core-concept.html#goner-%E9%80%9D%E8%80%85",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts - Goners"),n("OutboundLink")],1),t._v(", we mentioned that Goners can be injected into properties of other structures. Here, configuration, as a special case, can also be injected into properties of other structures. Its implementation is through "),n("a",{attrs:{href:"https://goner.fun/guide/core-concept.html#%F0%9F%A7%9B%F0%9F%8F%BB%E2%80%8D%E2%99%80%EF%B8%8Fvampire",target:"_blank",rel:"noopener noreferrer"}},[t._v("Vampire"),n("OutboundLink")],1),t._v(", if interested, you can refer to "),n("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.5/goner/config",target:"_blank",rel:"noopener noreferrer"}},[t._v("this part of the source code"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("p",[t._v("The supported types for properties are listed below:")]),t._v(" "),n("ol",[n("li",[t._v("Basic types\n"),n("ul",[n("li",[t._v("Boolean: bool")]),t._v(" "),n("li",[t._v("Integer types: int, int64, int32, int16, int8")]),t._v(" "),n("li",[t._v("Unsigned integer types: uint, uint64, uint32, uint16, uint8")]),t._v(" "),n("li",[t._v("Floating-point types: float64, float32")]),t._v(" "),n("li",[t._v("String type: string")])])])]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("For integer types and unsigned integer types, be cautious of the range they represent. If the number is too large, overflow may occur; for example, "),n("code",[t._v("int8")]),t._v(" represents the range "),n("code",[t._v("-128")]),t._v(" to "),n("code",[t._v("127")]),t._v(", so if a value larger than 127 is assigned, overflow will occur (assigning 128 will result in -128).")])]),t._v(" "),n("ol",{attrs:{start:"2"}},[n("li",[t._v("time.Duration\nFor convenient time parsing, "),n("code",[t._v("time.Duration")]),t._v(" type is supported in the configuration, and the following units can be used:\n"),n("ul",[n("li",[t._v("ns: nanoseconds")]),t._v(" "),n("li",[t._v("us: microseconds")]),t._v(" "),n("li",[t._v("ms: milliseconds")]),t._v(" "),n("li",[t._v("s: seconds")]),t._v(" "),n("li",[t._v("m: minutes")]),t._v(" "),n("li",[t._v("h: hours")])])])]),t._v(" "),n("p",[t._v("In the example, "),n("code",[t._v("my.conf.duration=10h")]),t._v(" represents 10 hours; "),n("code",[t._v("1h10m10s")]),t._v(" represents 1 hour, 10 minutes, and 10 seconds.")]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("Implementation uses "),n("code",[t._v("time.ParseDuration")]),t._v(", so you can refer to: "),n("a",{attrs:{href:"https://pkg.go.dev/time#ParseDuration",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://pkg.go.dev/time#ParseDuration"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("ol",{attrs:{start:"3"}},[n("li",[t._v("Struct types\nIn the example, the type of the configuration struct is provided. It needs to adhere to the following rules:\n"),n("ul",[n("li",[t._v("The properties in the struct to be configured must be public, i.e., starting with a capital letter.")]),t._v(" "),n("li",[t._v("Use the "),n("code",[t._v("properties")]),t._v(" tag to specify the name of the property.")]),t._v(" "),n("li",[t._v("Supports nesting.")])])])]),t._v(" "),n("p",[t._v("Explanation: In the example, the property "),n("code",[t._v("sub")]),t._v(" of "),n("code",[t._v("UseConfig")]),t._v(" reads the configuration key "),n("code",[t._v("my.conf.sub")]),t._v("; the type of "),n("code",[t._v("sub")]),t._v(" is "),n("code",[t._v("SubConf")]),t._v(", and the property "),n("code",[t._v("X")]),t._v(" is marked with the configuration name "),n("code",[t._v('properties:"x"')]),t._v(", so the value of "),n("code",[t._v("sub.X")]),t._v(" will be read from the configuration item "),n("code",[t._v("my.conf.sub.x")]),t._v(".")]),t._v(" "),n("ol",{attrs:{start:"4"}},[n("li",[t._v("Array types\nConfiguration injection supports the Slice type, and currently, the elements of the Slice support structs and pointers to structs. The key format for configuration is as follows:")])]),t._v(" "),n("div",{staticClass:"language-ini extra-class"},[n("pre",{pre:!0,attrs:{class:"language-ini"}},[n("code",[t._v("${injectConfigkey}[${index}].${structAttributeName}\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Explanation")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("${injectConfigkey}")]),t._v(" is the key of the item to be injected.")]),t._v(" "),n("li",[n("code",[t._v("${index}")]),t._v(" is the array index.")]),t._v(" "),n("li",[n("code",[t._v("${structAttributeName}")]),t._v(" is the value annotated on the property of the injected struct with the "),n("code",[t._v("properties")]),t._v(" tag.")])])]),t._v(" "),n("ol",{attrs:{start:"5"}},[n("li",[t._v("Pointer types\nSupports pointer types for injected properties.")])]),t._v(" "),n("h3",{attrs:{id:"configuration-files"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-files"}},[t._v("#")]),t._v(" Configuration Files")]),t._v(" "),n("h4",{attrs:{id:"configuration-file-directory"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-file-directory"}},[t._v("#")]),t._v(" Configuration File Directory")]),t._v(" "),n("p",[t._v("The directory where configuration files are stored is determined by three parts:")]),t._v(" "),n("ol",[n("li",[t._v("The executable file and the current working directory of the program.")]),t._v(" "),n("li",[t._v("Relative configuration directory\nThe relative configuration directory defaults to "),n("code",[t._v("config")]),t._v(", and can be changed by passing the "),n("code",[t._v("--conf $configDir")]),t._v(" parameter during startup.")])]),t._v(" "),n("p",[t._v("For example, if the compiled gone program is saved as "),n("code",[t._v("/app/gone-app")]),t._v(", and we run it from the "),n("code",[t._v("/home/degfy")]),t._v(" directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" /home/degfy/\n/app/gone-app\n")])])]),n("p",[t._v("And if the relative configuration directory is not modified by passing parameters, the configuration directory for the program would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config")]),t._v(" "),n("li",[t._v("/home/degfy/config")])]),t._v(" "),n("h4",{attrs:{id:"default-configuration-file-path"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#default-configuration-file-path"}},[t._v("#")]),t._v(" Default Configuration File Path")]),t._v(" "),n("p",[t._v("Continuing from the previous example, the paths for configuration files would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config/default.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/default.properties\nThese configuration files contain default configurations that do not change with the environment.")])]),t._v(" "),n("h4",{attrs:{id:"environment-specific-configuration-file-paths"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#environment-specific-configuration-file-paths"}},[t._v("#")]),t._v(" Environment-specific Configuration File Paths")]),t._v(" "),n("p",[t._v("During development, we typically use environment-specific configurations, such as:")]),t._v(" "),n("ul",[n("li",[t._v("local: Local development environment")]),t._v(" "),n("li",[t._v("dev: Development environment online")]),t._v(" "),n("li",[t._v("test: Testing environment online")]),t._v(" "),n("li",[t._v("prod: Production environment")])]),t._v(" "),n("p",[t._v("We can specify the current environment by setting the "),n("code",[t._v("ENV")]),t._v(" environment variable or passing the "),n("code",[t._v("--env $env")]),t._v(" parameter during startup. If neither is set, the default environment is "),n("code",[t._v("local")]),t._v(".\nContinuing from the previous example, the configuration files related to the environment would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config/local.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),n("h4",{attrs:{id:"configuration-loading-order"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-loading-order"}},[t._v("#")]),t._v(" Configuration Loading Order")]),t._v(" "),n("p",[t._v("As mentioned earlier, there are four configuration files when the program is started, and they are loaded in the following order:")]),t._v(" "),n("ol",[n("li",[t._v("/app/config/default.properties")]),t._v(" "),n("li",[t._v("/app/config/local.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/default.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),n("p",[t._v("If the same configuration item exists in multiple files, the value from the file loaded later will override the value from the file loaded earlier. That is, environment-specific configurations override default configurations, and configurations from the current running directory override those from the program directory.")]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Best Practices")]),t._v(" "),n("ol",[n("li",[t._v("Put all configurations in the default configuration file and override them in environment-specific configuration files as needed.")]),t._v(" "),n("li",[t._v('Design configurations with minimalism in mind and consider using a "convention over configuration" approach, where default values are provided whenever possible.')]),t._v(" "),n("li",[t._v('To make the code "ready out of the box," it\'s recommended to set up a complete configuration in '),n("code",[t._v("local.properties")]),t._v(" so that the program can run without any additional configuration, facilitating smooth startup for other team members who clone the codebase.")])])])])}),[],!1,null,null,null);n.default=s.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[32],{317:function(t,n,e){"use strict";e.r(n);var a=e(10),s=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"configuring-with-built-in-goners-support"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuring-with-built-in-goners-support"}},[t._v("#")]),t._v(" Configuring with Built-in Goners Support")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#example"}},[t._v("Example")]),n("ul",[n("li",[n("a",{attrs:{href:"#_1-create-a-mod"}},[t._v("1. Create a mod")])]),n("li",[n("a",{attrs:{href:"#_2-add-a-configuration-file"}},[t._v("2. Add a configuration file")])]),n("li",[n("a",{attrs:{href:"#_3-add-code"}},[t._v("3. Add code")])]),n("li",[n("a",{attrs:{href:"#_4-run"}},[t._v("4. Run")])])])]),n("li",[n("a",{attrs:{href:"#configuration-injection"}},[t._v("Configuration Injection")]),n("ul",[n("li",[n("a",{attrs:{href:"#tag-format-used"}},[t._v("Tag Format Used")])]),n("li",[n("a",{attrs:{href:"#supported-data-types-for-injection"}},[t._v("Supported Data Types for Injection")])]),n("li",[n("a",{attrs:{href:"#configuration-files"}},[t._v("Configuration Files")])])])])])]),n("p"),t._v(" "),n("p",[t._v("Gone provides a method to read configuration files using the built-in Goners. Currently, the configuration file format is only supported in "),n("code",[t._v(".properties")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"example"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#example"}},[t._v("#")]),t._v(" Example")]),t._v(" "),n("blockquote",[n("p",[t._v("You can find the source code of the example "),n("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-config",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("h3",{attrs:{id:"_1-create-a-mod"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_1-create-a-mod"}},[t._v("#")]),t._v(" 1. Create a mod")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go mod init use-config\n")])])]),n("h3",{attrs:{id:"_2-add-a-configuration-file"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_2-add-a-configuration-file"}},[t._v("#")]),t._v(" 2. Add a configuration file")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" config/default.properties\n")])])]),n("p",[t._v("The content of the "),n("code",[t._v("config/default.properties")]),t._v(" file is as follows:")]),t._v(" "),n("div",{staticClass:"language-properties extra-class"},[n("pre",{pre:!0,attrs:{class:"language-properties"}},[n("code",[n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int8")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("130")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10.222")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("config test")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.bool")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("true")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.duration")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10h")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("100x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("200y")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000y")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].x")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111x")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].y")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),n("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111y")]),t._v("\n")])])]),n("h3",{attrs:{id:"_3-add-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_3-add-code"}},[t._v("#")]),t._v(" 3. Add code")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" main.go\n")])])]),n("p",[t._v("The content of the "),n("code",[t._v("main.go")]),t._v(" file is as follows:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SubConf "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tX "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"x"`')]),t._v("\n\tY "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"y"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" UseConfig "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v("\n\tprintInt "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer to int")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.float64"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.string"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.bool"`')]),t._v("\n\tduration time"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.duration"`')]),t._v("\n\tdefaultV "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.default,default=ok"`')]),t._v("\n\n\tsub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SubConf "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.sub"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// pointer to struct")]),t._v("\n\n\tsubs "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("SubConf "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.subs"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// array")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("UseConfig"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int8=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"printInt=%d\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("printInt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"float64=%f\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string=%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"bool=%t\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"duration=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("duration"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"defaultV=%s\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("defaultV"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"sub.x=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("sub"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"subs=%v\\n"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("subs"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("UseConfig"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h3",{attrs:{id:"_4-run"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#_4-run"}},[t._v("#")]),t._v(" 4. Run")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go mod tidy\ngo run main.go\n")])])]),n("p",[t._v("The output will be as follows:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("int=10\nint8=-126\nprintInt=130\nfloat64=10.222000\nstring=config test\nbool=true\nduration=10h0m0s\ndefaultV=ok\nsub.x=&{100x 200y}\nsubs=[{0000x 0000y} {1111x 1111y}]\n")])])]),n("h2",{attrs:{id:"configuration-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-injection"}},[t._v("#")]),t._v(" Configuration Injection")]),t._v(" "),n("p",[t._v('As seen from the example, configuration items can also be "dependency injected" into Goners.')]),t._v(" "),n("h3",{attrs:{id:"tag-format-used"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#tag-format-used"}},[t._v("#")]),t._v(" Tag Format Used")]),t._v(" "),n("p",[t._v("The tag format for configuration injection is as follows:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v('gone:"config,${key},default=${defaultValue}"\n')])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Explanation:")]),t._v(" "),n("ol",[n("li",[t._v("The tag name remains "),n("code",[t._v("gone")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v("config")]),t._v(" is a fixed value, indicating that the property is a configuration.")]),t._v(" "),n("li",[n("code",[t._v("${key}")]),t._v(" represents the key in the configuration file.")]),t._v(" "),n("li",[n("code",[t._v("default=${defaultValue}")]),t._v(" is used to specify a default value, which can be omitted; "),n("code",[t._v("${defaultValue}")]),t._v(" is the default value. If no default value is specified and the corresponding key is missing in the configuration file, an error will be raised.")])])]),t._v(" "),n("h3",{attrs:{id:"supported-data-types-for-injection"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#supported-data-types-for-injection"}},[t._v("#")]),t._v(" Supported Data Types for Injection")]),t._v(" "),n("blockquote",[n("p",[t._v("In the "),n("a",{attrs:{href:"https://goner.fun/guide/core-concept.html#goner-%E9%80%9D%E8%80%85",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts - Goners"),n("OutboundLink")],1),t._v(", we mentioned that Goners can be injected into properties of other structures. Here, configuration, as a special case, can also be injected into properties of other structures. Its implementation is through "),n("a",{attrs:{href:"https://goner.fun/guide/core-concept.html#%F0%9F%A7%9B%F0%9F%8F%BB%E2%80%8D%E2%99%80%EF%B8%8Fvampire",target:"_blank",rel:"noopener noreferrer"}},[t._v("Vampire"),n("OutboundLink")],1),t._v(", if interested, you can refer to "),n("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.5/goner/config",target:"_blank",rel:"noopener noreferrer"}},[t._v("this part of the source code"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("p",[t._v("The supported types for properties are listed below:")]),t._v(" "),n("ol",[n("li",[t._v("Basic types\n"),n("ul",[n("li",[t._v("Boolean: bool")]),t._v(" "),n("li",[t._v("Integer types: int, int64, int32, int16, int8")]),t._v(" "),n("li",[t._v("Unsigned integer types: uint, uint64, uint32, uint16, uint8")]),t._v(" "),n("li",[t._v("Floating-point types: float64, float32")]),t._v(" "),n("li",[t._v("String type: string")])])])]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("For integer types and unsigned integer types, be cautious of the range they represent. If the number is too large, overflow may occur; for example, "),n("code",[t._v("int8")]),t._v(" represents the range "),n("code",[t._v("-128")]),t._v(" to "),n("code",[t._v("127")]),t._v(", so if a value larger than 127 is assigned, overflow will occur (assigning 128 will result in -128).")])]),t._v(" "),n("ol",{attrs:{start:"2"}},[n("li",[t._v("time.Duration\nFor convenient time parsing, "),n("code",[t._v("time.Duration")]),t._v(" type is supported in the configuration, and the following units can be used:\n"),n("ul",[n("li",[t._v("ns: nanoseconds")]),t._v(" "),n("li",[t._v("us: microseconds")]),t._v(" "),n("li",[t._v("ms: milliseconds")]),t._v(" "),n("li",[t._v("s: seconds")]),t._v(" "),n("li",[t._v("m: minutes")]),t._v(" "),n("li",[t._v("h: hours")])])])]),t._v(" "),n("p",[t._v("In the example, "),n("code",[t._v("my.conf.duration=10h")]),t._v(" represents 10 hours; "),n("code",[t._v("1h10m10s")]),t._v(" represents 1 hour, 10 minutes, and 10 seconds.")]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("Implementation uses "),n("code",[t._v("time.ParseDuration")]),t._v(", so you can refer to: "),n("a",{attrs:{href:"https://pkg.go.dev/time#ParseDuration",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://pkg.go.dev/time#ParseDuration"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("ol",{attrs:{start:"3"}},[n("li",[t._v("Struct types\nIn the example, the type of the configuration struct is provided. It needs to adhere to the following rules:\n"),n("ul",[n("li",[t._v("The properties in the struct to be configured must be public, i.e., starting with a capital letter.")]),t._v(" "),n("li",[t._v("Use the "),n("code",[t._v("properties")]),t._v(" tag to specify the name of the property.")]),t._v(" "),n("li",[t._v("Supports nesting.")])])])]),t._v(" "),n("p",[t._v("Explanation: In the example, the property "),n("code",[t._v("sub")]),t._v(" of "),n("code",[t._v("UseConfig")]),t._v(" reads the configuration key "),n("code",[t._v("my.conf.sub")]),t._v("; the type of "),n("code",[t._v("sub")]),t._v(" is "),n("code",[t._v("SubConf")]),t._v(", and the property "),n("code",[t._v("X")]),t._v(" is marked with the configuration name "),n("code",[t._v('properties:"x"')]),t._v(", so the value of "),n("code",[t._v("sub.X")]),t._v(" will be read from the configuration item "),n("code",[t._v("my.conf.sub.x")]),t._v(".")]),t._v(" "),n("ol",{attrs:{start:"4"}},[n("li",[t._v("Array types\nConfiguration injection supports the Slice type, and currently, the elements of the Slice support structs and pointers to structs. The key format for configuration is as follows:")])]),t._v(" "),n("div",{staticClass:"language-ini extra-class"},[n("pre",{pre:!0,attrs:{class:"language-ini"}},[n("code",[t._v("${injectConfigkey}[${index}].${structAttributeName}\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Explanation")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("${injectConfigkey}")]),t._v(" is the key of the item to be injected.")]),t._v(" "),n("li",[n("code",[t._v("${index}")]),t._v(" is the array index.")]),t._v(" "),n("li",[n("code",[t._v("${structAttributeName}")]),t._v(" is the value annotated on the property of the injected struct with the "),n("code",[t._v("properties")]),t._v(" tag.")])])]),t._v(" "),n("ol",{attrs:{start:"5"}},[n("li",[t._v("Pointer types\nSupports pointer types for injected properties.")])]),t._v(" "),n("h3",{attrs:{id:"configuration-files"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-files"}},[t._v("#")]),t._v(" Configuration Files")]),t._v(" "),n("h4",{attrs:{id:"configuration-file-directory"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-file-directory"}},[t._v("#")]),t._v(" Configuration File Directory")]),t._v(" "),n("p",[t._v("The directory where configuration files are stored is determined by three parts:")]),t._v(" "),n("ol",[n("li",[t._v("The executable file and the current working directory of the program.")]),t._v(" "),n("li",[t._v("Relative configuration directory\nThe relative configuration directory defaults to "),n("code",[t._v("config")]),t._v(", and can be changed by passing the "),n("code",[t._v("--conf $configDir")]),t._v(" parameter during startup.")])]),t._v(" "),n("p",[t._v("For example, if the compiled gone program is saved as "),n("code",[t._v("/app/gone-app")]),t._v(", and we run it from the "),n("code",[t._v("/home/degfy")]),t._v(" directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" /home/degfy/\n/app/gone-app\n")])])]),n("p",[t._v("And if the relative configuration directory is not modified by passing parameters, the configuration directory for the program would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config")]),t._v(" "),n("li",[t._v("/home/degfy/config")])]),t._v(" "),n("h4",{attrs:{id:"default-configuration-file-path"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#default-configuration-file-path"}},[t._v("#")]),t._v(" Default Configuration File Path")]),t._v(" "),n("p",[t._v("Continuing from the previous example, the paths for configuration files would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config/default.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/default.properties\nThese configuration files contain default configurations that do not change with the environment.")])]),t._v(" "),n("h4",{attrs:{id:"environment-specific-configuration-file-paths"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#environment-specific-configuration-file-paths"}},[t._v("#")]),t._v(" Environment-specific Configuration File Paths")]),t._v(" "),n("p",[t._v("During development, we typically use environment-specific configurations, such as:")]),t._v(" "),n("ul",[n("li",[t._v("local: Local development environment")]),t._v(" "),n("li",[t._v("dev: Development environment online")]),t._v(" "),n("li",[t._v("test: Testing environment online")]),t._v(" "),n("li",[t._v("prod: Production environment")])]),t._v(" "),n("p",[t._v("We can specify the current environment by setting the "),n("code",[t._v("ENV")]),t._v(" environment variable or passing the "),n("code",[t._v("--env $env")]),t._v(" parameter during startup. If neither is set, the default environment is "),n("code",[t._v("local")]),t._v(".\nContinuing from the previous example, the configuration files related to the environment would be:")]),t._v(" "),n("ul",[n("li",[t._v("/app/config/local.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),n("h4",{attrs:{id:"configuration-loading-order"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#configuration-loading-order"}},[t._v("#")]),t._v(" Configuration Loading Order")]),t._v(" "),n("p",[t._v("As mentioned earlier, there are four configuration files when the program is started, and they are loaded in the following order:")]),t._v(" "),n("ol",[n("li",[t._v("/app/config/default.properties")]),t._v(" "),n("li",[t._v("/app/config/local.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/default.properties")]),t._v(" "),n("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),n("p",[t._v("If the same configuration item exists in multiple files, the value from the file loaded later will override the value from the file loaded earlier. That is, environment-specific configurations override default configurations, and configurations from the current running directory override those from the program directory.")]),t._v(" "),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("Best Practices")]),t._v(" "),n("ol",[n("li",[t._v("Put all configurations in the default configuration file and override them in environment-specific configuration files as needed.")]),t._v(" "),n("li",[t._v('Design configurations with minimalism in mind and consider using a "convention over configuration" approach, where default values are provided whenever possible.')]),t._v(" "),n("li",[t._v('To make the code "ready out of the box," it\'s recommended to set up a complete configuration in '),n("code",[t._v("local.properties")]),t._v(" so that the program can run without any additional configuration, facilitating smooth startup for other team members who clone the codebase.")])])])])}),[],!1,null,null,null);n.default=s.exports}}]); \ No newline at end of file diff --git a/assets/js/34.4f9bb42d.js b/assets/js/33.62ee3133.js similarity index 99% rename from assets/js/34.4f9bb42d.js rename to assets/js/33.62ee3133.js index 51b8f11..da93600 100644 --- a/assets/js/34.4f9bb42d.js +++ b/assets/js/33.62ee3133.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[34],{318:function(t,e,s){"use strict";s.r(e);var n=s(10),a=Object(n.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"core-concepts-of-gone"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#core-concepts-of-gone"}},[t._v("#")]),t._v(" Core Concepts of Gone")]),t._v(" "),e("p",[t._v("\"Our code, after all, is just lifeless matter, unless it's resurrected in "),e("strong",[t._v("Heaven")]),t._v("; for that, we need to "),e("strong",[t._v("bury")]),t._v(" it in a "),e("strong",[t._v("Cemetery")]),t._v('."')]),t._v(" "),e("h2",{attrs:{id:"goner"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#goner"}},[t._v("#")]),t._v(" Goner")]),t._v(" "),e("p",[t._v("In the Gone framework, the most fundamental and core concept is "),e("strong",[t._v("Goner")]),t._v(", which refers to a structure anonymously embedded with "),e("code",[t._v("gone.Flag")]),t._v(". For example:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("Goner serves as a component in the Gone framework and is crucial for implementing dependency injection:")]),t._v(" "),e("ol",[e("li",[t._v("A Goner can be injected as a property into other structures.")]),t._v(" "),e("li",[t._v("The properties of a Goner can be injected into other types.")])]),t._v(" "),e("p",[t._v("The reason for embedding a "),e("code",[t._v("gone.Flag")]),t._v(" is to limit the scope of dependency injection, ensuring that injection occurs only among Goners, thereby providing a unified pattern for component implementation in the Gone framework.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("Below is the source code for Goner and gone.Flag:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Flag "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Flag"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner represents the deceased.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Goner "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("As an interface, Goner demands that objects implementing it have a private method "),e("code",[t._v("goneFlag()")]),t._v(". Due to Go's visibility restrictions, it's not possible to implement the internally defined private method "),e("code",[t._v("goneFlag()")]),t._v(" outside the "),e("code",[t._v("github.com/gone-io/gone")]),t._v(" package. Therefore, a structure can only become a Goner by embedding "),e("code",[t._v("gone.Flag")]),t._v(". This might be a bit complex, but essentially, it means that implementing Goner can only be achieved by embedding "),e("code",[t._v("gone.Flag")]),t._v(", not by directly implementing the "),e("code",[t._v("goneFlag()")]),t._v(" method.")])]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("When referring to multiple Goners, we use the plural form "),e("strong",[t._v("Goners")]),t._v(".")])]),t._v(" "),e("p",[t._v("The Gone framework also includes three special types of Goners:")]),t._v(" "),e("h3",{attrs:{id:"🔮-prophet"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#🔮-prophet"}},[t._v("#")]),t._v(" 🔮 Prophet")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" that implements the "),e("strong",[e("code",[t._v("AfterRevive() AfterReviveError")])]),t._v(" method on regular "),e("strong",[t._v("Goners")]),t._v("; "),e("strong",[t._v("Prophet")]),t._v("'s "),e("strong",[t._v("AfterRevive")]),t._v(" is executed after a "),e("strong",[t._v("Goner")]),t._v(" is resurrected.")]),t._v(" "),e("p",[t._v("The Prophet interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Prophet represents the prophet.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Prophet "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is executed after the Goner is revived.")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" AfterReviveError\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"😇-angel"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#😇-angel"}},[t._v("#")]),t._v(" 😇 Angel")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" possessing the angelic powers of "),e("strong",[e("code",[t._v("Start(Cemetery) error")])]),t._v(" on its left wing and "),e("strong",[e("code",[t._v("Stop(Cemetery) error")])]),t._v(" on its right wing, responsible for initiating (allocating resources, starting a service) and terminating (stopping a service, reclaiming resources) tasks, respectively.")]),t._v(" "),e("p",[t._v("The Angel interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Angel "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stop")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"🧛🏻‍♀️-vampire"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#🧛🏻‍♀️-vampire"}},[t._v("#")]),t._v(" 🧛🏻‍♀️ Vampire")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" with a unique ability — sucking with "),e("strong",[e("code",[t._v("Suck(conf string, v reflect.Value) SuckError")])]),t._v(". "),e("strong",[t._v("Suck")]),t._v(" enables injecting values that are not Goners into Goners' properties.")]),t._v(" "),e("p",[t._v("The Vampire interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SuckError "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Vampire "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Suck")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v reflect"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" SuckError\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"cemetery"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#cemetery"}},[t._v("#")]),t._v(" Cemetery")]),t._v(" "),e("p",[t._v("Cemetery is used to manage Goners, primarily providing methods for "),e("strong",[t._v("Burying")]),t._v(" and "),e("strong",[t._v("Reviving")]),t._v(" Goners. Its interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cemetery "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ... Other methods")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("GonerId"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Cemetery "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burying a Goner in the Cemetery")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ReviveAllFromTombs revives all Goners.")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReviveAllFromTombs")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("From the code, it's evident that Cemetery itself is a Goner, automatically buried and resurrected when the Gone framework starts.")]),t._v(" "),e("h3",{attrs:{id:"burying"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#burying"}},[t._v("#")]),t._v(" Burying")]),t._v(" "),e("p",[t._v("Burying a Goner in a Cemetery means registering the Goner with the framework for later property injection. In code implementation, "),e("strong",[t._v("Bury")]),t._v(" is a public method of "),e("strong",[t._v("Cemetery")]),t._v(", typically called through the "),e("strong",[t._v("Priest")]),t._v(" function.")]),t._v(" "),e("h3",{attrs:{id:"reviving"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#reviving"}},[t._v("#")]),t._v(" Reviving")]),t._v(" "),e("p",[t._v("Reviving entails completing the injection of properties required by a Goner. In the "),e("code",[t._v("ReviveAllFromTombs() error")]),t._v(" function, all Goners buried in the Cemetery are attempted to be revived. If any property injection fails, the program panics.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("After reviving all Goners, "),e("strong",[t._v("ReviveAllFromTombs")]),t._v(" calls the "),e("strong",[t._v("AfterRevive")]),t._v(" method of all "),e("strong",[t._v("Prophets")]),t._v(".")])]),t._v(" "),e("h2",{attrs:{id:"heaven"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#heaven"}},[t._v("#")]),t._v(" Heaven")]),t._v(" "),e("p",[t._v("Heaven represents a Gone program, responsible for managing the program's startup, shutdown, and associated processes (resurrection completed before startup). It facilitates executing certain hook tasks before and after startup and before program termination. Heaven accepts a priest function to begin its operation, as shown below:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call cemetery.Bury to bury Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or call other Priest functions")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("Or:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call cemetery.Bury to bury Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or call other Priest functions")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TODO: Perform operations after startup")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"priest"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#priest"}},[t._v("#")]),t._v(" Priest")]),t._v(" "),e("p",[t._v("Priest is a function responsible for burying Goners in the Cemetery. Its definition is as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Priest "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])]),e("p",[t._v("In the implementation of the "),e("strong",[t._v("Priest")]),t._v(" function, you can call "),e("strong",[t._v("cemetery.Bury")]),t._v(" to accomplish this, as shown below:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous burial, not specifying the GonerId of the buried Goner")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"X"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("p",[t._v("Alternatively, you can accomplish this by calling other "),e("strong",[t._v("Priest")]),t._v(" functions:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("If we develop a component package where multiple "),e("strong",[t._v("Goners")]),t._v(" are used to accomplish various functionalities, and we need to "),e("strong",[t._v("bury")]),t._v(" these "),e("strong",[t._v("Goners")]),t._v(" simultaneously when using them, we can write a "),e("strong",[t._v("Priest")]),t._v(" function to facilitate bulk "),e("strong",[t._v("burying")]),t._v(" of these "),e("strong",[t._v("Goners")]),t._v(" in business code.")]),t._v(" "),e("p",[t._v("That's exactly what we do with the built-in component package of the framework. Here's how it's done, along with the "),e("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("code"),e("OutboundLink")],1),t._v(" and "),e("a",{attrs:{href:"https://goner.fun/goners/#%E6%A1%86%E6%9E%B6%E5%86%85%E7%BD%AEgoners",target:"_blank",rel:"noopener noreferrer"}},[t._v("documentation"),e("OutboundLink")],1),t._v(".")]),t._v(" "),e("p",[t._v("Additionally, we've developed a command-line utility called "),e("strong",[t._v("gone")]),t._v(", which scans for special comments "),e("code",[t._v("//go:gone")]),t._v(" to automatically generate "),e("strong",[t._v("Priest")]),t._v(" functions. You can refer to "),e("RouterLink",{attrs:{to:"/guide/auto-gen-priest.html"}},[t._v("Auto-generating Priest")]),t._v(" for more information.")],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[33],{320:function(t,e,s){"use strict";s.r(e);var n=s(10),a=Object(n.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"core-concepts-of-gone"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#core-concepts-of-gone"}},[t._v("#")]),t._v(" Core Concepts of Gone")]),t._v(" "),e("p",[t._v("\"Our code, after all, is just lifeless matter, unless it's resurrected in "),e("strong",[t._v("Heaven")]),t._v("; for that, we need to "),e("strong",[t._v("bury")]),t._v(" it in a "),e("strong",[t._v("Cemetery")]),t._v('."')]),t._v(" "),e("h2",{attrs:{id:"goner"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#goner"}},[t._v("#")]),t._v(" Goner")]),t._v(" "),e("p",[t._v("In the Gone framework, the most fundamental and core concept is "),e("strong",[t._v("Goner")]),t._v(", which refers to a structure anonymously embedded with "),e("code",[t._v("gone.Flag")]),t._v(". For example:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("Goner serves as a component in the Gone framework and is crucial for implementing dependency injection:")]),t._v(" "),e("ol",[e("li",[t._v("A Goner can be injected as a property into other structures.")]),t._v(" "),e("li",[t._v("The properties of a Goner can be injected into other types.")])]),t._v(" "),e("p",[t._v("The reason for embedding a "),e("code",[t._v("gone.Flag")]),t._v(" is to limit the scope of dependency injection, ensuring that injection occurs only among Goners, thereby providing a unified pattern for component implementation in the Gone framework.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("Below is the source code for Goner and gone.Flag:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Flag "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Flag"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner represents the deceased.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Goner "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("As an interface, Goner demands that objects implementing it have a private method "),e("code",[t._v("goneFlag()")]),t._v(". Due to Go's visibility restrictions, it's not possible to implement the internally defined private method "),e("code",[t._v("goneFlag()")]),t._v(" outside the "),e("code",[t._v("github.com/gone-io/gone")]),t._v(" package. Therefore, a structure can only become a Goner by embedding "),e("code",[t._v("gone.Flag")]),t._v(". This might be a bit complex, but essentially, it means that implementing Goner can only be achieved by embedding "),e("code",[t._v("gone.Flag")]),t._v(", not by directly implementing the "),e("code",[t._v("goneFlag()")]),t._v(" method.")])]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("When referring to multiple Goners, we use the plural form "),e("strong",[t._v("Goners")]),t._v(".")])]),t._v(" "),e("p",[t._v("The Gone framework also includes three special types of Goners:")]),t._v(" "),e("h3",{attrs:{id:"🔮-prophet"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#🔮-prophet"}},[t._v("#")]),t._v(" 🔮 Prophet")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" that implements the "),e("strong",[e("code",[t._v("AfterRevive() AfterReviveError")])]),t._v(" method on regular "),e("strong",[t._v("Goners")]),t._v("; "),e("strong",[t._v("Prophet")]),t._v("'s "),e("strong",[t._v("AfterRevive")]),t._v(" is executed after a "),e("strong",[t._v("Goner")]),t._v(" is resurrected.")]),t._v(" "),e("p",[t._v("The Prophet interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Prophet represents the prophet.")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Prophet "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// AfterRevive is executed after the Goner is revived.")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" AfterReviveError\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"😇-angel"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#😇-angel"}},[t._v("#")]),t._v(" 😇 Angel")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" possessing the angelic powers of "),e("strong",[e("code",[t._v("Start(Cemetery) error")])]),t._v(" on its left wing and "),e("strong",[e("code",[t._v("Stop(Cemetery) error")])]),t._v(" on its right wing, responsible for initiating (allocating resources, starting a service) and terminating (stopping a service, reclaiming resources) tasks, respectively.")]),t._v(" "),e("p",[t._v("The Angel interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Angel "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stop")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h3",{attrs:{id:"🧛🏻‍♀️-vampire"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#🧛🏻‍♀️-vampire"}},[t._v("#")]),t._v(" 🧛🏻‍♀️ Vampire")]),t._v(" "),e("p",[t._v("A special type of "),e("strong",[t._v("Goner")]),t._v(" with a unique ability — sucking with "),e("strong",[e("code",[t._v("Suck(conf string, v reflect.Value) SuckError")])]),t._v(". "),e("strong",[t._v("Suck")]),t._v(" enables injecting values that are not Goners into Goners' properties.")]),t._v(" "),e("p",[t._v("The Vampire interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SuckError "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Vampire "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Suck")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v reflect"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" SuckError\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"cemetery"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#cemetery"}},[t._v("#")]),t._v(" Cemetery")]),t._v(" "),e("p",[t._v("Cemetery is used to manage Goners, primarily providing methods for "),e("strong",[t._v("Burying")]),t._v(" and "),e("strong",[t._v("Reviving")]),t._v(" Goners. Its interface is defined as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cemetery "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ... Other methods")]),t._v("\n\tGoner\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("GonerId"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Cemetery "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Burying a Goner in the Cemetery")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ReviveAllFromTombs revives all Goners.")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReviveAllFromTombs")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("From the code, it's evident that Cemetery itself is a Goner, automatically buried and resurrected when the Gone framework starts.")]),t._v(" "),e("h3",{attrs:{id:"burying"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#burying"}},[t._v("#")]),t._v(" Burying")]),t._v(" "),e("p",[t._v("Burying a Goner in a Cemetery means registering the Goner with the framework for later property injection. In code implementation, "),e("strong",[t._v("Bury")]),t._v(" is a public method of "),e("strong",[t._v("Cemetery")]),t._v(", typically called through the "),e("strong",[t._v("Priest")]),t._v(" function.")]),t._v(" "),e("h3",{attrs:{id:"reviving"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#reviving"}},[t._v("#")]),t._v(" Reviving")]),t._v(" "),e("p",[t._v("Reviving entails completing the injection of properties required by a Goner. In the "),e("code",[t._v("ReviveAllFromTombs() error")]),t._v(" function, all Goners buried in the Cemetery are attempted to be revived. If any property injection fails, the program panics.")]),t._v(" "),e("div",{staticClass:"custom-block tip"},[e("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),e("p",[t._v("After reviving all Goners, "),e("strong",[t._v("ReviveAllFromTombs")]),t._v(" calls the "),e("strong",[t._v("AfterRevive")]),t._v(" method of all "),e("strong",[t._v("Prophets")]),t._v(".")])]),t._v(" "),e("h2",{attrs:{id:"heaven"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#heaven"}},[t._v("#")]),t._v(" Heaven")]),t._v(" "),e("p",[t._v("Heaven represents a Gone program, responsible for managing the program's startup, shutdown, and associated processes (resurrection completed before startup). It facilitates executing certain hook tasks before and after startup and before program termination. Heaven accepts a priest function to begin its operation, as shown below:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call cemetery.Bury to bury Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or call other Priest functions")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("Or:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call cemetery.Bury to bury Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or call other Priest functions")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TODO: Perform operations after startup")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"priest"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#priest"}},[t._v("#")]),t._v(" Priest")]),t._v(" "),e("p",[t._v("Priest is a function responsible for burying Goners in the Cemetery. Its definition is as follows:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Priest "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])]),e("p",[t._v("In the implementation of the "),e("strong",[t._v("Priest")]),t._v(" function, you can call "),e("strong",[t._v("cemetery.Bury")]),t._v(" to accomplish this, as shown below:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous burial, not specifying the GonerId of the buried Goner")]),t._v("\n\tcemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"X"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("p",[t._v("Alternatively, you can accomplish this by calling other "),e("strong",[t._v("Priest")]),t._v(" functions:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("p",[t._v("If we develop a component package where multiple "),e("strong",[t._v("Goners")]),t._v(" are used to accomplish various functionalities, and we need to "),e("strong",[t._v("bury")]),t._v(" these "),e("strong",[t._v("Goners")]),t._v(" simultaneously when using them, we can write a "),e("strong",[t._v("Priest")]),t._v(" function to facilitate bulk "),e("strong",[t._v("burying")]),t._v(" of these "),e("strong",[t._v("Goners")]),t._v(" in business code.")]),t._v(" "),e("p",[t._v("That's exactly what we do with the built-in component package of the framework. Here's how it's done, along with the "),e("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("code"),e("OutboundLink")],1),t._v(" and "),e("a",{attrs:{href:"https://goner.fun/goners/#%E6%A1%86%E6%9E%B6%E5%86%85%E7%BD%AEgoners",target:"_blank",rel:"noopener noreferrer"}},[t._v("documentation"),e("OutboundLink")],1),t._v(".")]),t._v(" "),e("p",[t._v("Additionally, we've developed a command-line utility called "),e("strong",[t._v("gone")]),t._v(", which scans for special comments "),e("code",[t._v("//go:gone")]),t._v(" to automatically generate "),e("strong",[t._v("Priest")]),t._v(" functions. You can refer to "),e("RouterLink",{attrs:{to:"/guide/auto-gen-priest.html"}},[t._v("Auto-generating Priest")]),t._v(" for more information.")],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/35.ecaa3b68.js b/assets/js/34.346cf91f.js similarity index 99% rename from assets/js/35.ecaa3b68.js rename to assets/js/34.346cf91f.js index 1624ae1..f369462 100644 --- a/assets/js/35.ecaa3b68.js +++ b/assets/js/34.346cf91f.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[35],{320:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone-s-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-s-hook-functions"}},[t._v("#")]),t._v(" Gone's Hook Functions")]),t._v(" "),s("h2",{attrs:{id:"what-are-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#what-are-hook-functions"}},[t._v("#")]),t._v(" What are Hook Functions?")]),t._v(" "),s("p",[t._v("In the process of starting and stopping Gone, support is provided to register four types of functions that will be executed at specific times, known as Hook functions:")]),t._v(" "),s("ul",[s("li",[t._v("BeforeStart Hook function: Executes before Gone starts, registered using "),s("code",[t._v("BeforeStart")]),t._v(".")]),t._v(" "),s("li",[t._v("AfterStart Hook function: Executes after Gone starts, registered using "),s("code",[t._v("AfterStart")]),t._v(".")]),t._v(" "),s("li",[t._v("BeforeStop Hook function: Executes before Gone stops, registered using "),s("code",[t._v("BeforeStop")]),t._v(".")]),t._v(" "),s("li",[t._v("AfterStop Hook function: Executes after Gone stops, registered using "),s("code",[t._v("AfterStop")]),t._v(".")])]),t._v(" "),s("h2",{attrs:{id:"code-example"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#code-example"}},[t._v("#")]),t._v(" Code Example")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"First BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tworker Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"worker-bob"`')]),t._v("\n\t\t\tboss Boss "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Second BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Boss:"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Worker:"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Third BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("The output of the above code is as follows:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("Third BeforeStart function\nSecond BeforeStart function\nBoss: Jim\nWorker: Bob\nFirst BeforeStart function\n")])])]),s("h3",{attrs:{id:"rules-for-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#rules-for-hook-functions"}},[t._v("#")]),t._v(" Rules for Hook Functions")]),t._v(" "),s("ol",[s("li",[t._v("Multiple registrations of the same type of Hook function are allowed.")]),t._v(" "),s("li",[t._v("For "),s("code",[t._v("BeforeStart")]),t._v(" and "),s("code",[t._v("BeforeStop")]),t._v(" Hook functions, the ones registered later are executed first.")]),t._v(" "),s("li",[t._v("For "),s("code",[t._v("AfterStart")]),t._v(" and "),s("code",[t._v("AfterStop")]),t._v(" Hook functions, the ones registered earlier are executed first.")]),t._v(" "),s("li",[t._v("Hook functions can be registered using the "),s("code",[t._v("Preparer")]),t._v(" object returned by "),s("code",[t._v("gone.Prepare(priest)")]),t._v(", supporting chaining.")]),t._v(" "),s("li",[t._v("The types of functions that can be registered with the "),s("code",[t._v("Preparer")]),t._v(" object include:\n"),s("ul",[s("li",[s("strong",[t._v("Input Parameters:")]),t._v(" "),s("ul",[s("li",[t._v("No parameters")]),t._v(" "),s("li",[t._v("Anonymous struct parameters, with properties annotated with "),s("code",[t._v("gone")]),t._v(" tags automatically injected with corresponding values.")])])]),t._v(" "),s("li",[s("strong",[t._v("Return Parameters:")]),t._v(" "),s("ul",[s("li",[t._v("No return parameters")]),t._v(" "),s("li",[s("code",[t._v("error")]),t._v(" parameter; if the error parameter is not "),s("code",[t._v("nil")]),t._v(", the program will panic when it reaches the Hook, terminating execution.")])])])])])]),t._v(" "),s("h2",{attrs:{id:"gone-s-startup-process"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-s-startup-process"}},[t._v("#")]),t._v(" Gone's Startup Process")]),t._v(" "),s("ol",[s("li",[t._v("Revive all "),s("strong",[t._v("buried")]),t._v(" Goners.")]),t._v(" "),s("li",[t._v("Execute the "),s("code",[t._v("AfterRevive")]),t._v(" method of all prophet Goners.")]),t._v(" "),s("li",[t._v("Register the "),s("code",[t._v("Start")]),t._v(" method of "),s("strong",[t._v("angels")]),t._v(" as BeforeStart Hook functions and the "),s("code",[t._v("Stop")]),t._v(" method as BeforeStop Hook functions.")]),t._v(" "),s("li",[t._v("Register Hook functions for business code.")]),t._v(" "),s("li",[t._v("Execute BeforeStart Hook functions in order.")]),t._v(" "),s("li",[t._v("Execute AfterStart Hook functions in order.")]),t._v(" "),s("li",[t._v("Wait for the program to end.")]),t._v(" "),s("li",[t._v("Execute BeforeStop Hook functions in order.")]),t._v(" "),s("li",[t._v("Execute AfterStop Hook functions in order.")])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[34],{319:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone-s-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-s-hook-functions"}},[t._v("#")]),t._v(" Gone's Hook Functions")]),t._v(" "),s("h2",{attrs:{id:"what-are-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#what-are-hook-functions"}},[t._v("#")]),t._v(" What are Hook Functions?")]),t._v(" "),s("p",[t._v("In the process of starting and stopping Gone, support is provided to register four types of functions that will be executed at specific times, known as Hook functions:")]),t._v(" "),s("ul",[s("li",[t._v("BeforeStart Hook function: Executes before Gone starts, registered using "),s("code",[t._v("BeforeStart")]),t._v(".")]),t._v(" "),s("li",[t._v("AfterStart Hook function: Executes after Gone starts, registered using "),s("code",[t._v("AfterStart")]),t._v(".")]),t._v(" "),s("li",[t._v("BeforeStop Hook function: Executes before Gone stops, registered using "),s("code",[t._v("BeforeStop")]),t._v(".")]),t._v(" "),s("li",[t._v("AfterStop Hook function: Executes after Gone stops, registered using "),s("code",[t._v("AfterStop")]),t._v(".")])]),t._v(" "),s("h2",{attrs:{id:"code-example"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#code-example"}},[t._v("#")]),t._v(" Code Example")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"First BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tworker Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"worker-bob"`')]),t._v("\n\t\t\tboss Boss "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Second BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Boss:"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Worker:"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BeforeStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Third BeforeStart function"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("The output of the above code is as follows:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("Third BeforeStart function\nSecond BeforeStart function\nBoss: Jim\nWorker: Bob\nFirst BeforeStart function\n")])])]),s("h3",{attrs:{id:"rules-for-hook-functions"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#rules-for-hook-functions"}},[t._v("#")]),t._v(" Rules for Hook Functions")]),t._v(" "),s("ol",[s("li",[t._v("Multiple registrations of the same type of Hook function are allowed.")]),t._v(" "),s("li",[t._v("For "),s("code",[t._v("BeforeStart")]),t._v(" and "),s("code",[t._v("BeforeStop")]),t._v(" Hook functions, the ones registered later are executed first.")]),t._v(" "),s("li",[t._v("For "),s("code",[t._v("AfterStart")]),t._v(" and "),s("code",[t._v("AfterStop")]),t._v(" Hook functions, the ones registered earlier are executed first.")]),t._v(" "),s("li",[t._v("Hook functions can be registered using the "),s("code",[t._v("Preparer")]),t._v(" object returned by "),s("code",[t._v("gone.Prepare(priest)")]),t._v(", supporting chaining.")]),t._v(" "),s("li",[t._v("The types of functions that can be registered with the "),s("code",[t._v("Preparer")]),t._v(" object include:\n"),s("ul",[s("li",[s("strong",[t._v("Input Parameters:")]),t._v(" "),s("ul",[s("li",[t._v("No parameters")]),t._v(" "),s("li",[t._v("Anonymous struct parameters, with properties annotated with "),s("code",[t._v("gone")]),t._v(" tags automatically injected with corresponding values.")])])]),t._v(" "),s("li",[s("strong",[t._v("Return Parameters:")]),t._v(" "),s("ul",[s("li",[t._v("No return parameters")]),t._v(" "),s("li",[s("code",[t._v("error")]),t._v(" parameter; if the error parameter is not "),s("code",[t._v("nil")]),t._v(", the program will panic when it reaches the Hook, terminating execution.")])])])])])]),t._v(" "),s("h2",{attrs:{id:"gone-s-startup-process"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-s-startup-process"}},[t._v("#")]),t._v(" Gone's Startup Process")]),t._v(" "),s("ol",[s("li",[t._v("Revive all "),s("strong",[t._v("buried")]),t._v(" Goners.")]),t._v(" "),s("li",[t._v("Execute the "),s("code",[t._v("AfterRevive")]),t._v(" method of all prophet Goners.")]),t._v(" "),s("li",[t._v("Register the "),s("code",[t._v("Start")]),t._v(" method of "),s("strong",[t._v("angels")]),t._v(" as BeforeStart Hook functions and the "),s("code",[t._v("Stop")]),t._v(" method as BeforeStop Hook functions.")]),t._v(" "),s("li",[t._v("Register Hook functions for business code.")]),t._v(" "),s("li",[t._v("Execute BeforeStart Hook functions in order.")]),t._v(" "),s("li",[t._v("Execute AfterStart Hook functions in order.")]),t._v(" "),s("li",[t._v("Wait for the program to end.")]),t._v(" "),s("li",[t._v("Execute BeforeStop Hook functions in order.")]),t._v(" "),s("li",[t._v("Execute AfterStop Hook functions in order.")])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/36.9f41478f.js b/assets/js/35.29971508.js similarity index 99% rename from assets/js/36.9f41478f.js rename to assets/js/35.29971508.js index de8a092..6fedd53 100644 --- a/assets/js/36.9f41478f.js +++ b/assets/js/35.29971508.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[36],{323:function(t,n,s){"use strict";s.r(n);var e=s(10),a=Object(e.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"how-to-gracefully-use-built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-gracefully-use-built-in-goners"}},[t._v("#")]),t._v(" How to Gracefully Use Built-in Goners?")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#built-in-goners"}},[t._v("Built-in Goners")])]),n("li",[n("a",{attrs:{href:"#the-priest-function"}},[t._v("The Priest Function")])]),n("li",[n("a",{attrs:{href:"#how-to-use"}},[t._v("How to Use")])]),n("li",[n("a",{attrs:{href:"#goneid-for-built-in-goners"}},[t._v("GoneId for Built-in Goners")])])])]),n("p"),t._v(" "),n("h2",{attrs:{id:"built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#built-in-goners"}},[t._v("#")]),t._v(" Built-in Goners")]),t._v(" "),n("p",[t._v("To lower the entry barrier for using Gone, we have developed some "),n("RouterLink",{attrs:{to:"/goners/#built-in-framework-goners"}},[t._v("built-in Goners")]),t._v(" to provide basic functionality.")],1),t._v(" "),n("h2",{attrs:{id:"the-priest-function"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#the-priest-function"}},[t._v("#")]),t._v(" The "),n("code",[t._v("Priest")]),t._v(" Function")]),t._v(" "),n("p",[t._v("The "),n("code",[t._v("Priest")]),t._v(" function is defined within the gone framework, and it allows for the bulk burial of Goners. Additionally, the "),n("code",[t._v("Priest")]),t._v(" function can be nested, meaning A can call B, and B can call C. Therefore, after developing a set of features, you can provide a "),n("code",[t._v("Priest")]),t._v(" function that buries all the Goners used.")]),t._v(" "),n("p",[t._v("In fact, when developing built-in Goners for the framework, we adopt this approach. Additionally, for ease of use, we have defined some shortcuts in "),n("code",[t._v("goner/priest.go")]),t._v(", as shown below:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" goner\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/tracer"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/urllib"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" tracer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" gin"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" xorm"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" redis"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("UrllibPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" urllib"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"how-to-use"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-use"}},[t._v("#")]),t._v(" How to Use")]),t._v(" "),n("p",[t._v("This way, if we want to use basic logging, tracing, and configuration functionality, we only need to:")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("Add "),n("code",[t._v("gone.BasePriest(cemetery)")]),t._v(" in the main "),n("code",[t._v("Priest")]),t._v(" function:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury Goners related to logging, tracing, and configuration")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call other Priests")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[t._v("If logging is needed, inject it into the necessary Goners:")])])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoService")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoService"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoService "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlogrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection to nested anonymous property")]),t._v("\n\n\tlog logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection to nested log property")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoService"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using inherited method")]),t._v("\n\tsvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using method on property")]),t._v("\n\tsvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("When injecting built-in components, named injection should be used.")])]),t._v(" "),n("h2",{attrs:{id:"goneid-for-built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goneid-for-built-in-goners"}},[t._v("#")]),t._v(" GoneId for Built-in Goners")]),t._v(" "),n("p",[t._v("The GonerId for built-in Goners is centrally defined in "),n("code",[t._v("https://github.com/gone-io/gone/blob/main/ids.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gone\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ID for built-in components in the Gone framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneHeaven ID of the Heaven component, which represents the program itself and is automatically injected when the Gone program starts")]),t._v("\n\tIdGoneHeaven "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-heaven"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneCemetery ID of the Cemetery component, which is essential for completing dependency injection and is automatically injected when the Gone program starts")]),t._v("\n\tIdGoneCemetery "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cemetery"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneTestKit ID of the TestKit component, which is injected into the program when calling gone.Test or gone.TestAt; it should not be injected into non-test code")]),t._v("\n\tIdGoneTestKit "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-test-kit"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Configuration, logging, and Tracer together form the foundation of the Gone framework, and can be buried together using [goner.BasePriest](goner#BasePriest)")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdConfig ID of the configuration Goner, providing configuration capability")]),t._v("\n\tIdConfig "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneConfigure ID of the configurator Goner")]),t._v("\n\tIdGoneConfigure "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-configure"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneTracer ID of the Tracer Goner, providing logging and tracing capabilities")]),t._v("\n\tIdGoneTracer "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-tracer"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneLogger ID of the Logger Goner, used for logging")]),t._v("\n\tIdGoneLogger "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-logger"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneCumx [cmux Goner](/goner/cmux#Server) ID")]),t._v("\n\tIdGoneCumx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cumx"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneGin IDs related to Gin components, which can be buried together using [goner.GinPriest](goner#GinPriest)")]),t._v("\n\tIdGoneGin "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin"')]),t._v("\n\tIdGoneGinRouter "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-router"')]),t._v("\n\tIdGoneGinProcessor "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-processor"')]),t._v("\n\tIdGoneGinProxy "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-proxy"')]),t._v("\n\tIdGoneGinResponser "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-responser"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneXorm ID of the Xorm Goner, encapsulating xorm for database operations; can be buried using [goner.XormPriest](goner#XormPriest)")]),t._v("\n\tIdGoneXorm "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-xorm"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneRedisPool ID of the redis pool Goner; Redis-related Goners can be buried using [goner.RedisPriest](goner#RedisPriest)")]),t._v("\n\tIdGoneRedisPool "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-pool"')]),t._v("\n\tIdGoneRedisCache "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-cache"')]),t._v("\n\tIdGoneRedisKey "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-key"')]),t._v("\n\tIdGoneRedisLocker "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-locker"')]),t._v("\n\tIdGoneRedisProvider "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-provider"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneSchedule ID of the Scheduler Goner; can be buried using [goner.SchedulePriest](goner#SchedulePriest)")]),t._v("\n\tIdGoneSchedule "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-schedule"')]),t._v("\n\n\tIdGoneReq "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-urllib"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[35],{322:function(t,n,s){"use strict";s.r(n);var e=s(10),a=Object(e.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"how-to-gracefully-use-built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-gracefully-use-built-in-goners"}},[t._v("#")]),t._v(" How to Gracefully Use Built-in Goners?")]),t._v(" "),n("p"),n("div",{staticClass:"table-of-contents"},[n("ul",[n("li",[n("a",{attrs:{href:"#built-in-goners"}},[t._v("Built-in Goners")])]),n("li",[n("a",{attrs:{href:"#the-priest-function"}},[t._v("The Priest Function")])]),n("li",[n("a",{attrs:{href:"#how-to-use"}},[t._v("How to Use")])]),n("li",[n("a",{attrs:{href:"#goneid-for-built-in-goners"}},[t._v("GoneId for Built-in Goners")])])])]),n("p"),t._v(" "),n("h2",{attrs:{id:"built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#built-in-goners"}},[t._v("#")]),t._v(" Built-in Goners")]),t._v(" "),n("p",[t._v("To lower the entry barrier for using Gone, we have developed some "),n("RouterLink",{attrs:{to:"/goners/#built-in-framework-goners"}},[t._v("built-in Goners")]),t._v(" to provide basic functionality.")],1),t._v(" "),n("h2",{attrs:{id:"the-priest-function"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#the-priest-function"}},[t._v("#")]),t._v(" The "),n("code",[t._v("Priest")]),t._v(" Function")]),t._v(" "),n("p",[t._v("The "),n("code",[t._v("Priest")]),t._v(" function is defined within the gone framework, and it allows for the bulk burial of Goners. Additionally, the "),n("code",[t._v("Priest")]),t._v(" function can be nested, meaning A can call B, and B can call C. Therefore, after developing a set of features, you can provide a "),n("code",[t._v("Priest")]),t._v(" function that buries all the Goners used.")]),t._v(" "),n("p",[t._v("In fact, when developing built-in Goners for the framework, we adopt this approach. Additionally, for ease of use, we have defined some shortcuts in "),n("code",[t._v("goner/priest.go")]),t._v(", as shown below:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" goner\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/tracer"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/urllib"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" tracer"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" gin"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" xorm"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" redis"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("UrllibPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" urllib"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"how-to-use"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#how-to-use"}},[t._v("#")]),t._v(" How to Use")]),t._v(" "),n("p",[t._v("This way, if we want to use basic logging, tracing, and configuration functionality, we only need to:")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("Add "),n("code",[t._v("gone.BasePriest(cemetery)")]),t._v(" in the main "),n("code",[t._v("Priest")]),t._v(" function:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury Goners related to logging, tracing, and configuration")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Call other Priests")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),n("li",[n("p",[t._v("If logging is needed, inject it into the necessary Goners:")])])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoService")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoService"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoService "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlogrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection to nested anonymous property")]),t._v("\n\n\tlog logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection to nested log property")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoService"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using inherited method")]),t._v("\n\tsvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using method on property")]),t._v("\n\tsvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("div",{staticClass:"custom-block tip"},[n("p",{staticClass:"custom-block-title"},[t._v("TIP")]),t._v(" "),n("p",[t._v("When injecting built-in components, named injection should be used.")])]),t._v(" "),n("h2",{attrs:{id:"goneid-for-built-in-goners"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#goneid-for-built-in-goners"}},[t._v("#")]),t._v(" GoneId for Built-in Goners")]),t._v(" "),n("p",[t._v("The GonerId for built-in Goners is centrally defined in "),n("code",[t._v("https://github.com/gone-io/gone/blob/main/ids.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gone\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ID for built-in components in the Gone framework")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneHeaven ID of the Heaven component, which represents the program itself and is automatically injected when the Gone program starts")]),t._v("\n\tIdGoneHeaven "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-heaven"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneCemetery ID of the Cemetery component, which is essential for completing dependency injection and is automatically injected when the Gone program starts")]),t._v("\n\tIdGoneCemetery "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cemetery"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneTestKit ID of the TestKit component, which is injected into the program when calling gone.Test or gone.TestAt; it should not be injected into non-test code")]),t._v("\n\tIdGoneTestKit "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-test-kit"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Configuration, logging, and Tracer together form the foundation of the Gone framework, and can be buried together using [goner.BasePriest](goner#BasePriest)")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdConfig ID of the configuration Goner, providing configuration capability")]),t._v("\n\tIdConfig "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneConfigure ID of the configurator Goner")]),t._v("\n\tIdGoneConfigure "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-configure"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneTracer ID of the Tracer Goner, providing logging and tracing capabilities")]),t._v("\n\tIdGoneTracer "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-tracer"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneLogger ID of the Logger Goner, used for logging")]),t._v("\n\tIdGoneLogger "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-logger"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneCumx [cmux Goner](/goner/cmux#Server) ID")]),t._v("\n\tIdGoneCumx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cumx"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneGin IDs related to Gin components, which can be buried together using [goner.GinPriest](goner#GinPriest)")]),t._v("\n\tIdGoneGin "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin"')]),t._v("\n\tIdGoneGinRouter "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-router"')]),t._v("\n\tIdGoneGinProcessor "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-processor"')]),t._v("\n\tIdGoneGinProxy "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-proxy"')]),t._v("\n\tIdGoneGinResponser "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-responser"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneXorm ID of the Xorm Goner, encapsulating xorm for database operations; can be buried using [goner.XormPriest](goner#XormPriest)")]),t._v("\n\tIdGoneXorm "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-xorm"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneRedisPool ID of the redis pool Goner; Redis-related Goners can be buried using [goner.RedisPriest](goner#RedisPriest)")]),t._v("\n\tIdGoneRedisPool "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-pool"')]),t._v("\n\tIdGoneRedisCache "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-cache"')]),t._v("\n\tIdGoneRedisKey "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-key"')]),t._v("\n\tIdGoneRedisLocker "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-locker"')]),t._v("\n\tIdGoneRedisProvider "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-provider"')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneSchedule ID of the Scheduler Goner; can be buried using [goner.SchedulePriest](goner#SchedulePriest)")]),t._v("\n\tIdGoneSchedule "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-schedule"')]),t._v("\n\n\tIdGoneReq "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-urllib"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/37.4c81140b.js b/assets/js/36.d16f93ad.js similarity index 99% rename from assets/js/37.4c81140b.js rename to assets/js/36.d16f93ad.js index 1faf145..8de134a 100644 --- a/assets/js/37.4c81140b.js +++ b/assets/js/36.d16f93ad.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[37],{321:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"logging-output"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#logging-output"}},[t._v("#")]),t._v(" Logging Output")]),t._v(" "),n("p",[t._v("In Gone, an internal Goner is provided for logging output - "),n("a",{attrs:{href:"github.com/gone-io/gone/tree/main/goner/logrus"}},[t._v("logrus")]),t._v(", which is implemented by wrapping the "),n("code",[t._v("github.com/sirupsen/logrus")]),t._v(" package as a Goner. It is hoped that other logging output packages will be encapsulated by contributors to provide more logging solutions.")]),t._v(" "),n("h2",{attrs:{id:"burying-related-goners-in-the-cemetery"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#burying-related-goners-in-the-cemetery"}},[t._v("#")]),t._v(" Burying Related Goners in the Cemetery")]),t._v(" "),n("blockquote",[n("p",[t._v("tip: To understand the core concepts and terminology of Gone, please read: "),n("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),n("OutboundLink")],1)])]),t._v(" "),n("p",[t._v("Here, we use the "),n("strong",[n("code",[t._v("BasePriest")])]),t._v(" from the package "),n("strong",[n("code",[t._v("github.com/gone-io/gone/tree/main/goner")])]),t._v(" to bury related Goners. In the "),n("code",[t._v("BasePriest")]),t._v(", Goners related to configuration and trace are also buried into the Cemetery, as these three packages are commonly used together.")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury other Goners")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"adding-related-configurations-in-the-configuration-file"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#adding-related-configurations-in-the-configuration-file"}},[t._v("#")]),t._v(" Adding Related Configurations in the Configuration File")]),t._v(" "),n("blockquote",[n("p",[t._v("tip: "),n("a",{attrs:{href:"https://goner.fun/en/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Supporting Configuration Files through Built-in Goners"),n("OutboundLink")],1)])]),t._v(" "),n("p",[n("strong",[t._v("Supported Configuration Items")])]),t._v(" "),n("ul",[n("li",[t._v("log.level: Log level, default is info; supported levels:\n"),n("ul",[n("li",[t._v("panic")]),t._v(" "),n("li",[t._v("fatal")]),t._v(" "),n("li",[t._v("error")]),t._v(" "),n("li",[t._v("warn or warning")]),t._v(" "),n("li",[t._v("info")]),t._v(" "),n("li",[t._v("debug")]),t._v(" "),n("li",[t._v("trace")])])]),t._v(" "),n("li",[t._v("log.report-caller: If true, log will print the file name and line number of the log output position, for example:\n2024-05-11 09:09:57.784|INFO|"),n("strong",[t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46")]),t._v("|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080")]),t._v(" "),n("li",[t._v("log.output: Log output location, default is stdout (standard output), supports stderr and a file path\n"),n("blockquote",[n("p",[n("strong",[t._v("Best Practice:")]),t._v(" Deploy the application in containers and directly output logs to standard output, collected by a log collection component such as EFK.")])])])]),t._v(" "),n("h2",{attrs:{id:"logging"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#logging"}},[t._v("#")]),t._v(" Logging")]),t._v(" "),n("p",[t._v("Use the "),n("code",[t._v("Info")]),t._v(" method to log:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Log")]),t._v("\n svc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),n("p",[t._v("For logging at other levels, refer to the interface code:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Logger interface for logging")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Logger "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Tracef")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Trace")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debug")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Info")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Print")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warn")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warning")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panic")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Traceln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infoln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"log-format"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#log-format"}},[t._v("#")]),t._v(" Log Format")]),t._v(" "),n("p",[n("code",[t._v("${Log Output Time}|${Log Level}|${Source Code Position of Log Output}|${TraceId}|${Log Content}")])]),t._v(" "),n("p",[t._v("For example:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),n("h2",{attrs:{id:"about-traceid"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#about-traceid"}},[t._v("#")]),t._v(" About TraceId")]),t._v(" "),n("p",[t._v("In web applications, we want a unified ID to identify logs generated by the same request. This unified ID is the TraceId. If this ID exists, when troubleshooting, we can search for logs using this ID to obtain all logs related to the request.")])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[36],{324:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"logging-output"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#logging-output"}},[t._v("#")]),t._v(" Logging Output")]),t._v(" "),n("p",[t._v("In Gone, an internal Goner is provided for logging output - "),n("a",{attrs:{href:"github.com/gone-io/gone/tree/main/goner/logrus"}},[t._v("logrus")]),t._v(", which is implemented by wrapping the "),n("code",[t._v("github.com/sirupsen/logrus")]),t._v(" package as a Goner. It is hoped that other logging output packages will be encapsulated by contributors to provide more logging solutions.")]),t._v(" "),n("h2",{attrs:{id:"burying-related-goners-in-the-cemetery"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#burying-related-goners-in-the-cemetery"}},[t._v("#")]),t._v(" Burying Related Goners in the Cemetery")]),t._v(" "),n("blockquote",[n("p",[t._v("tip: To understand the core concepts and terminology of Gone, please read: "),n("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),n("OutboundLink")],1)])]),t._v(" "),n("p",[t._v("Here, we use the "),n("strong",[n("code",[t._v("BasePriest")])]),t._v(" from the package "),n("strong",[n("code",[t._v("github.com/gone-io/gone/tree/main/goner")])]),t._v(" to bury related Goners. In the "),n("code",[t._v("BasePriest")]),t._v(", Goners related to configuration and trace are also buried into the Cemetery, as these three packages are commonly used together.")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury other Goners")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"adding-related-configurations-in-the-configuration-file"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#adding-related-configurations-in-the-configuration-file"}},[t._v("#")]),t._v(" Adding Related Configurations in the Configuration File")]),t._v(" "),n("blockquote",[n("p",[t._v("tip: "),n("a",{attrs:{href:"https://goner.fun/en/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Supporting Configuration Files through Built-in Goners"),n("OutboundLink")],1)])]),t._v(" "),n("p",[n("strong",[t._v("Supported Configuration Items")])]),t._v(" "),n("ul",[n("li",[t._v("log.level: Log level, default is info; supported levels:\n"),n("ul",[n("li",[t._v("panic")]),t._v(" "),n("li",[t._v("fatal")]),t._v(" "),n("li",[t._v("error")]),t._v(" "),n("li",[t._v("warn or warning")]),t._v(" "),n("li",[t._v("info")]),t._v(" "),n("li",[t._v("debug")]),t._v(" "),n("li",[t._v("trace")])])]),t._v(" "),n("li",[t._v("log.report-caller: If true, log will print the file name and line number of the log output position, for example:\n2024-05-11 09:09:57.784|INFO|"),n("strong",[t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46")]),t._v("|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080")]),t._v(" "),n("li",[t._v("log.output: Log output location, default is stdout (standard output), supports stderr and a file path\n"),n("blockquote",[n("p",[n("strong",[t._v("Best Practice:")]),t._v(" Deploy the application in containers and directly output logs to standard output, collected by a log collection component such as EFK.")])])])]),t._v(" "),n("h2",{attrs:{id:"logging"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#logging"}},[t._v("#")]),t._v(" Logging")]),t._v(" "),n("p",[t._v("Use the "),n("code",[t._v("Info")]),t._v(" method to log:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Log")]),t._v("\n svc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),n("p",[t._v("For logging at other levels, refer to the interface code:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Logger interface for logging")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Logger "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Tracef")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicf")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Trace")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debug")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Info")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Print")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warn")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warning")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panic")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Traceln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infoln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicln")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"log-format"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#log-format"}},[t._v("#")]),t._v(" Log Format")]),t._v(" "),n("p",[n("code",[t._v("${Log Output Time}|${Log Level}|${Source Code Position of Log Output}|${TraceId}|${Log Content}")])]),t._v(" "),n("p",[t._v("For example:")]),t._v(" "),n("div",{staticClass:"language- extra-class"},[n("pre",{pre:!0,attrs:{class:"language-text"}},[n("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),n("h2",{attrs:{id:"about-traceid"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#about-traceid"}},[t._v("#")]),t._v(" About TraceId")]),t._v(" "),n("p",[t._v("In web applications, we want a unified ID to identify logs generated by the same request. This unified ID is the TraceId. If this ID exists, when troubleshooting, we can search for logs using this ID to obtain all logs related to the request.")])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/38.bb9b17c3.js b/assets/js/37.f42bd49c.js similarity index 99% rename from assets/js/38.bb9b17c3.js rename to assets/js/37.f42bd49c.js index 057cd70..2921f47 100644 --- a/assets/js/38.bb9b17c3.js +++ b/assets/js/37.f42bd49c.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[38],{324:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"using-redis-for-distributed-locking-and-caching"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-redis-for-distributed-locking-and-caching"}},[t._v("#")]),t._v(" Using Redis for Distributed Locking and Caching")]),t._v(" "),s("p",[t._v('In this article, we will share how to use distributed caching and distributed locking in Gone, where distributed locking includes a more flexible handling method — "smart lock". This method locks a processing function, periodically checks the remaining expiration time of the lock during function execution, and automatically renews the lock if necessary. The function automatically unlocks after execution.')]),t._v(" "),s("h2",{attrs:{id:"step-1-embedding-redis-related-goner-into-cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-1-embedding-redis-related-goner-into-cemetery"}},[t._v("#")]),t._v(" Step 1: Embedding Redis Related Goner into Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("What is a Goner?\nWhat does it mean to bury?\nWhat is a Cemetery?\nRefer to "),s("a",{attrs:{href:"https://goner.fun/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("In the Priest function, add "),s("code",[t._v("_ = goner.RedisPriest(cemetery)")]),t._v(" as follows:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using the goner.RedisPriest function to bury Redis-related Goner into Cemetery")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"step-2-adding-redis-configuration-to-the-configuration-file"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-2-adding-redis-configuration-to-the-configuration-file"}},[t._v("#")]),t._v(" Step 2: Adding Redis Configuration to the Configuration File")]),t._v(" "),s("p",[t._v("Create a configuration file "),s("code",[t._v("config/default.properties")]),t._v(" with the following contents:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Redis service address in the format `host:port`")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.server")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("localhost:6379")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Redis service password, default is empty if not configured")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.password")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("Note: The Redis service address should be set to a Redis service you can access.")])]),t._v(" "),s("p",[t._v("Additional configuration options:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("redis.max-idle")]),t._v(": maximum number of idle connections, default is 2 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.max-active")]),t._v(": maximum number of active connections, default is 10 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.db")]),t._v(": the database to use, default is 0 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.cache.prefix")]),t._v(": key prefix, if set, all CRUD operations on Redis will append this prefix using the format "),s("code",[t._v("${prefix}#${key}")]),t._v("; default is empty")])]),t._v(" "),s("blockquote",[s("p",[t._v("For more on configuration files, refer to: "),s("a",{attrs:{href:"https://goner.fun/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Support for Configuration Files Through Built-in Goners"),s("OutboundLink")],1)])]),t._v(" "),s("h2",{attrs:{id:"step-3-using-redis"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-3-using-redis"}},[t._v("#")]),t._v(" Step 3: Using Redis")]),t._v(" "),s("h3",{attrs:{id:"injecting-interfaces"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#injecting-interfaces"}},[t._v("#")]),t._v(" Injecting Interfaces")]),t._v(" "),s("p",[t._v("Inject the interfaces "),s("code",[t._v("redis.Cache")]),t._v(" and "),s("code",[t._v("redis.Locker")]),t._v(" into the structure where they are needed, their "),s("code",[t._v("GonerId")]),t._v(" are respectively: "),s("code",[t._v("gone-redis-cache")]),t._v(" and "),s("code",[t._v("gone-redis-locker")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"using-distributed-cache"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-distributed-cache"}},[t._v("#")]),t._v(" Using Distributed Cache")]),t._v(" "),s("p",[t._v("Refer to the comments in the code below:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Setting the cache")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the value to cache, type any; the passed value will be encoded to `[]byte` and sent to Redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The third parameter is the expiration time, type `time.Duration`; if omitted, no expiration time is set")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Getting the cache")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is a pointer to receive the cached value, type any; the value retrieved from Redis will be decoded into the type of the passed pointer")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Other methods on the interface:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("Remove(key string) (err error)")]),t._v(": used to delete a specific Redis key, supports wildcard")]),t._v(" "),s("li",[s("code",[t._v("Keys(key string) ([]string, error)")]),t._v(": used to find keys using a prefix or wildcard, ⚠️ use this method with caution")]),t._v(" "),s("li",[s("code",[t._v("Prefix() string")]),t._v(": get the current cache configuration's key prefix")])]),t._v(" "),s("h3",{attrs:{id:"using-distributed-lock"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-distributed-lock"}},[t._v("#")]),t._v(" Using Distributed Lock")]),t._v(" "),s("ol",[s("li",[t._v("Locking for a specific duration")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Attempt to acquire the lock and lock it for a duration")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first returned parameter is a function to unlock")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the lock key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the lock's expiration time, type `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// After operations, you need to unlock")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Once the lock is acquired,business operations can be performed.")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2.")]),t._v(" Locking a function where the lock is automatically renewed "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" the operation does not finish"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" and it unlocks automatically once the operation is complete\n"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("``")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`go\nfunc (r *redisUser) LockFunc() {\n\tlockKey := "gone-lock-key"\n\terr := r.locker.LockAndDo(\n\t\tlockKey, // The first parameter is the lock key, type `')]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`\n\t\tfunc() { // The second parameter is the function to be executed, type `")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`, representing an operation\n\t\t\t// Once the lock is acquired, business operations can be performed\n\t\t\t//todo\n\t\t\tprintln("do some options")\n\t\t},\n\t\t100*time.Second, // The third parameter is the lock\'s expiration time, type `')]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`; both the initial locking and subsequent renewals will use this value\n\t\t5*time.Second, // The fourth parameter is the interval for lock renewal, type `")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration`"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" periodically checks "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" the lock is about to expire"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" and renews the lock "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" it is within the next period\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("This approach is quite intelligent and is referred to as a “smart lock.” It is recommended for its ease of use and reduction in cognitive load.")]),t._v(" "),s("h2",{attrs:{id:"complete-example-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#complete-example-code"}},[t._v("#")]),t._v(" Complete Example Code")]),t._v(" "),s("p",[t._v("The source code for the example can be found "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-redis",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),s("OutboundLink")],1)]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using the goner.RedisPriest function to embed Redis-related Goner into the Cemetery")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Setting the cache")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the value to cache, type any; the passed value will be encoded to `[]byte` and sent to Redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The third parameter is the expiration time, type `time.Duration`; if omitted, no expiration time is set")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Getting the cache")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is a pointer to receive the cached value, type any; the value retrieved from Redis will be decoded into the type of the passed pointer")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Attempting to obtain the lock and lock it for a specific duration")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first returned parameter is a function to unlock")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the lock key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the lock's expiration time, type `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// After operations, you need to unlock")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Once the lock is acquired, business operations can be performed")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[37],{336:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"using-redis-for-distributed-locking-and-caching"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-redis-for-distributed-locking-and-caching"}},[t._v("#")]),t._v(" Using Redis for Distributed Locking and Caching")]),t._v(" "),s("p",[t._v('In this article, we will share how to use distributed caching and distributed locking in Gone, where distributed locking includes a more flexible handling method — "smart lock". This method locks a processing function, periodically checks the remaining expiration time of the lock during function execution, and automatically renews the lock if necessary. The function automatically unlocks after execution.')]),t._v(" "),s("h2",{attrs:{id:"step-1-embedding-redis-related-goner-into-cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-1-embedding-redis-related-goner-into-cemetery"}},[t._v("#")]),t._v(" Step 1: Embedding Redis Related Goner into Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("What is a Goner?\nWhat does it mean to bury?\nWhat is a Cemetery?\nRefer to "),s("a",{attrs:{href:"https://goner.fun/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("In the Priest function, add "),s("code",[t._v("_ = goner.RedisPriest(cemetery)")]),t._v(" as follows:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using the goner.RedisPriest function to bury Redis-related Goner into Cemetery")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"step-2-adding-redis-configuration-to-the-configuration-file"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-2-adding-redis-configuration-to-the-configuration-file"}},[t._v("#")]),t._v(" Step 2: Adding Redis Configuration to the Configuration File")]),t._v(" "),s("p",[t._v("Create a configuration file "),s("code",[t._v("config/default.properties")]),t._v(" with the following contents:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Redis service address in the format `host:port`")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.server")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("localhost:6379")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Redis service password, default is empty if not configured")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.password")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("Note: The Redis service address should be set to a Redis service you can access.")])]),t._v(" "),s("p",[t._v("Additional configuration options:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("redis.max-idle")]),t._v(": maximum number of idle connections, default is 2 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.max-active")]),t._v(": maximum number of active connections, default is 10 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.db")]),t._v(": the database to use, default is 0 if not set")]),t._v(" "),s("li",[s("code",[t._v("redis.cache.prefix")]),t._v(": key prefix, if set, all CRUD operations on Redis will append this prefix using the format "),s("code",[t._v("${prefix}#${key}")]),t._v("; default is empty")])]),t._v(" "),s("blockquote",[s("p",[t._v("For more on configuration files, refer to: "),s("a",{attrs:{href:"https://goner.fun/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Support for Configuration Files Through Built-in Goners"),s("OutboundLink")],1)])]),t._v(" "),s("h2",{attrs:{id:"step-3-using-redis"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#step-3-using-redis"}},[t._v("#")]),t._v(" Step 3: Using Redis")]),t._v(" "),s("h3",{attrs:{id:"injecting-interfaces"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#injecting-interfaces"}},[t._v("#")]),t._v(" Injecting Interfaces")]),t._v(" "),s("p",[t._v("Inject the interfaces "),s("code",[t._v("redis.Cache")]),t._v(" and "),s("code",[t._v("redis.Locker")]),t._v(" into the structure where they are needed, their "),s("code",[t._v("GonerId")]),t._v(" are respectively: "),s("code",[t._v("gone-redis-cache")]),t._v(" and "),s("code",[t._v("gone-redis-locker")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"using-distributed-cache"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-distributed-cache"}},[t._v("#")]),t._v(" Using Distributed Cache")]),t._v(" "),s("p",[t._v("Refer to the comments in the code below:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Setting the cache")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the value to cache, type any; the passed value will be encoded to `[]byte` and sent to Redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The third parameter is the expiration time, type `time.Duration`; if omitted, no expiration time is set")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Getting the cache")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is a pointer to receive the cached value, type any; the value retrieved from Redis will be decoded into the type of the passed pointer")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Other methods on the interface:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("Remove(key string) (err error)")]),t._v(": used to delete a specific Redis key, supports wildcard")]),t._v(" "),s("li",[s("code",[t._v("Keys(key string) ([]string, error)")]),t._v(": used to find keys using a prefix or wildcard, ⚠️ use this method with caution")]),t._v(" "),s("li",[s("code",[t._v("Prefix() string")]),t._v(": get the current cache configuration's key prefix")])]),t._v(" "),s("h3",{attrs:{id:"using-distributed-lock"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#using-distributed-lock"}},[t._v("#")]),t._v(" Using Distributed Lock")]),t._v(" "),s("ol",[s("li",[t._v("Locking for a specific duration")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Attempt to acquire the lock and lock it for a duration")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first returned parameter is a function to unlock")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the lock key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the lock's expiration time, type `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// After operations, you need to unlock")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Once the lock is acquired,business operations can be performed.")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2.")]),t._v(" Locking a function where the lock is automatically renewed "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" the operation does not finish"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" and it unlocks automatically once the operation is complete\n"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("``")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`go\nfunc (r *redisUser) LockFunc() {\n\tlockKey := "gone-lock-key"\n\terr := r.locker.LockAndDo(\n\t\tlockKey, // The first parameter is the lock key, type `')]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`\n\t\tfunc() { // The second parameter is the function to be executed, type `")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`, representing an operation\n\t\t\t// Once the lock is acquired, business operations can be performed\n\t\t\t//todo\n\t\t\tprintln("do some options")\n\t\t},\n\t\t100*time.Second, // The third parameter is the lock\'s expiration time, type `')]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`; both the initial locking and subsequent renewals will use this value\n\t\t5*time.Second, // The fourth parameter is the interval for lock renewal, type `")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration`"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" periodically checks "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" the lock is about to expire"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" and renews the lock "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" it is within the next period\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("This approach is quite intelligent and is referred to as a “smart lock.” It is recommended for its ease of use and reduction in cognitive load.")]),t._v(" "),s("h2",{attrs:{id:"complete-example-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#complete-example-code"}},[t._v("#")]),t._v(" Complete Example Code")]),t._v(" "),s("p",[t._v("The source code for the example can be found "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-redis",target:"_blank",rel:"noopener noreferrer"}},[t._v("here"),s("OutboundLink")],1)]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Using the goner.RedisPriest function to embed Redis-related Goner into the Cemetery")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Setting the cache")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the value to cache, type any; the passed value will be encoded to `[]byte` and sent to Redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The third parameter is the expiration time, type `time.Duration`; if omitted, no expiration time is set")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Getting the cache")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the cache key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is a pointer to receive the cached value, type any; the value retrieved from Redis will be decoded into the type of the passed pointer")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Attempting to obtain the lock and lock it for a specific duration")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first returned parameter is a function to unlock")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The first parameter is the lock key, type `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The second parameter is the lock's expiration time, type `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// After operations, you need to unlock")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Once the lock is acquired, business operations can be performed")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/39.68305519.js b/assets/js/38.1b8b11cd.js similarity index 99% rename from assets/js/39.68305519.js rename to assets/js/38.1b8b11cd.js index 5c08ed8..7f8b95b 100644 --- a/assets/js/39.68305519.js +++ b/assets/js/38.1b8b11cd.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[39],{325:function(t,e,s){"use strict";s.r(e);var a=s(10),n=Object(a.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"trace-log-with-traceid"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#trace-log-with-traceid"}},[t._v("#")]),t._v(" Trace Log with traceId")]),t._v(" "),e("p",[t._v("In web applications, a single request may go through many business processes. To facilitate troubleshooting, we want all logs generated by different business processes to have a unified traceId. Having a traceId allows us to link all logs related to the entire business process, making it easier to trace and analyze where problems occur in the business flow.")]),t._v(" "),e("p",[t._v("In other open-source frameworks, it's generally recommended to pass a "),e("code",[t._v("context.Context")]),t._v(" parameter to "),e("strong",[t._v("all")]),t._v(" functions, which is also recommended by the official Golang documentation. However, we believe that adding this extra parameter to every function is burdensome. We aim to avoid passing additional parameters to every function just to print a traceId in logs. Therefore, in Gone, we provide a built-in Goner to achieve this functionality.")]),t._v(" "),e("h2",{attrs:{id:"burying-related-goners-in-the-cemetery"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#burying-related-goners-in-the-cemetery"}},[t._v("#")]),t._v(" Burying Related Goners in the Cemetery")]),t._v(" "),e("blockquote",[e("p",[t._v("tip: To understand the core concepts and terminology of Gone, please read: "),e("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),e("OutboundLink")],1)])]),t._v(" "),e("p",[t._v("Here, we use the "),e("strong",[e("code",[t._v("BasePriest")])]),t._v(" from the package "),e("strong",[e("code",[t._v("github.com/gone-io/gone/tree/main/goner")])]),t._v(" to bury related Goners. In the "),e("code",[t._v("BasePriest")]),t._v(", Goners related to "),e("code",[t._v("tracer")]),t._v(", "),e("code",[t._v("config")]),t._v(", and "),e("code",[t._v("logrus")]),t._v(" are all buried into the Cemetery, as these three packages are commonly used together.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury other Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"simple-usage"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#simple-usage"}},[t._v("#")]),t._v(" Simple Usage")]),t._v(" "),e("p",[t._v("When the tracer is buried, when we print logs using the injected "),e("code",[t._v("logrus.Logger")]),t._v(" interface, a traceId will automatically be added to the logs.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Print log")]),t._v("\n\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("p",[t._v("For example, the "),e("code",[t._v("061ad00f-8c0d-479c-bc4c-393e0cf2cca2")]),t._v(" is the traceId:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),e("h2",{attrs:{id:"passing-traceid-across-goroutines"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#passing-traceid-across-goroutines"}},[t._v("#")]),t._v(" Passing TraceId Across Goroutines")]),t._v(" "),e("p",[t._v("In the previous example, it's normal if no new goroutines are used. If a new goroutine is started using the "),e("code",[t._v("go")]),t._v(" keyword, it will be noticed that the logs printed by the goroutine do not have a traceId. To solve this, inject the "),e("code",[t._v("tracer.Tracer")]),t._v(" interface and use the "),e("code",[t._v("Go")]),t._v(" method instead of the "),e("code",[t._v("go")]),t._v(" keyword to start a new goroutine.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n tracer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Tracer "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-tracer"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject tracer")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Go")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Print log in a new goroutine")]),t._v("\n\t\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"log in new goroutine"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("h2",{attrs:{id:"passing-traceid-across-processes-services"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#passing-traceid-across-processes-services"}},[t._v("#")]),t._v(" Passing TraceId Across Processes/Services")]),t._v(" "),e("p",[t._v("In microservices, a web request typically spans multiple microservices. Cross-service communication is generally done through:")]),t._v(" "),e("ol",[e("li",[t._v("Message middleware\nTo facilitate the passing of traceId in the message middleware and to facilitate the use of the message middleware to pass business events, we have open-sourced the "),e("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://github.com/gone-io/emitter"),e("OutboundLink")],1),t._v(" repository. In this repository, we have implemented an adapter for "),e("strong",[t._v("Rocket MQ")]),t._v(", and plan to adapt to other mainstream message middleware such as "),e("strong",[t._v("Kafka")]),t._v(" and "),e("strong",[t._v("RabbitMQ")]),t._v(" in the future.")]),t._v(" "),e("li",[t._v("RPC calls/internal http calls\nUse the built-in Goner "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/urllib",target:"_blank",rel:"noopener noreferrer"}},[t._v("urllib"),e("OutboundLink")],1),t._v(" to send http requests to Gone Web programs, and the traceId will be automatically passed between services. Additionally, using the built-in "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/grpc",target:"_blank",rel:"noopener noreferrer"}},[t._v("grpc"),e("OutboundLink")],1),t._v(" to implement gRPC calls will also automatically pass the traceId. More RPC call support will be provided in the future.")])]),t._v(" "),e("h2",{attrs:{id:"multi-language-support"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#multi-language-support"}},[t._v("#")]),t._v(" Multi-language Support")]),t._v(" "),e("p",[t._v("To pass the traceId in an HTTP request, a special header "),e("code",[t._v("X-Trace-ID")]),t._v(' is added to carry the traceId. Therefore, if multiple programming languages are used, as long as the different services follow the rule of "attaching '),e("code",[t._v("X-Trace-ID")]),t._v(" when making requests on the client side and parsing "),e("code",[t._v("X-Trace-ID")]),t._v(' when processing requests on the server side," traceId can be seamlessly passed across services developed in different languages.'),e("br"),t._v("\nWe plan to develop packages in other languages to seamlessly integrate with other languages in the future.")])])}),[],!1,null,null,null);e.default=n.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[38],{323:function(t,e,s){"use strict";s.r(e);var a=s(10),n=Object(a.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"trace-log-with-traceid"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#trace-log-with-traceid"}},[t._v("#")]),t._v(" Trace Log with traceId")]),t._v(" "),e("p",[t._v("In web applications, a single request may go through many business processes. To facilitate troubleshooting, we want all logs generated by different business processes to have a unified traceId. Having a traceId allows us to link all logs related to the entire business process, making it easier to trace and analyze where problems occur in the business flow.")]),t._v(" "),e("p",[t._v("In other open-source frameworks, it's generally recommended to pass a "),e("code",[t._v("context.Context")]),t._v(" parameter to "),e("strong",[t._v("all")]),t._v(" functions, which is also recommended by the official Golang documentation. However, we believe that adding this extra parameter to every function is burdensome. We aim to avoid passing additional parameters to every function just to print a traceId in logs. Therefore, in Gone, we provide a built-in Goner to achieve this functionality.")]),t._v(" "),e("h2",{attrs:{id:"burying-related-goners-in-the-cemetery"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#burying-related-goners-in-the-cemetery"}},[t._v("#")]),t._v(" Burying Related Goners in the Cemetery")]),t._v(" "),e("blockquote",[e("p",[t._v("tip: To understand the core concepts and terminology of Gone, please read: "),e("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Core Concepts of Gone"),e("OutboundLink")],1)])]),t._v(" "),e("p",[t._v("Here, we use the "),e("strong",[e("code",[t._v("BasePriest")])]),t._v(" from the package "),e("strong",[e("code",[t._v("github.com/gone-io/gone/tree/main/goner")])]),t._v(" to bury related Goners. In the "),e("code",[t._v("BasePriest")]),t._v(", Goners related to "),e("code",[t._v("tracer")]),t._v(", "),e("code",[t._v("config")]),t._v(", and "),e("code",[t._v("logrus")]),t._v(" are all buried into the Cemetery, as these three packages are commonly used together.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury other Goners")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),e("h2",{attrs:{id:"simple-usage"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#simple-usage"}},[t._v("#")]),t._v(" Simple Usage")]),t._v(" "),e("p",[t._v("When the tracer is buried, when we print logs using the injected "),e("code",[t._v("logrus.Logger")]),t._v(" interface, a traceId will automatically be added to the logs.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Print log")]),t._v("\n\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("p",[t._v("For example, the "),e("code",[t._v("061ad00f-8c0d-479c-bc4c-393e0cf2cca2")]),t._v(" is the traceId:")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),e("h2",{attrs:{id:"passing-traceid-across-goroutines"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#passing-traceid-across-goroutines"}},[t._v("#")]),t._v(" Passing TraceId Across Goroutines")]),t._v(" "),e("p",[t._v("In the previous example, it's normal if no new goroutines are used. If a new goroutine is started using the "),e("code",[t._v("go")]),t._v(" keyword, it will be noticed that the logs printed by the goroutine do not have a traceId. To solve this, inject the "),e("code",[t._v("tracer.Tracer")]),t._v(" interface and use the "),e("code",[t._v("Go")]),t._v(" method instead of the "),e("code",[t._v("go")]),t._v(" keyword to start a new goroutine.")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named injection into nested log attributes")]),t._v("\n tracer"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Tracer "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-tracer"`')]),t._v(" "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Inject tracer")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),e("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Go")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Print log in a new goroutine")]),t._v("\n\t\tsvc"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"log in new goroutine"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),e("h2",{attrs:{id:"passing-traceid-across-processes-services"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#passing-traceid-across-processes-services"}},[t._v("#")]),t._v(" Passing TraceId Across Processes/Services")]),t._v(" "),e("p",[t._v("In microservices, a web request typically spans multiple microservices. Cross-service communication is generally done through:")]),t._v(" "),e("ol",[e("li",[t._v("Message middleware\nTo facilitate the passing of traceId in the message middleware and to facilitate the use of the message middleware to pass business events, we have open-sourced the "),e("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://github.com/gone-io/emitter"),e("OutboundLink")],1),t._v(" repository. In this repository, we have implemented an adapter for "),e("strong",[t._v("Rocket MQ")]),t._v(", and plan to adapt to other mainstream message middleware such as "),e("strong",[t._v("Kafka")]),t._v(" and "),e("strong",[t._v("RabbitMQ")]),t._v(" in the future.")]),t._v(" "),e("li",[t._v("RPC calls/internal http calls\nUse the built-in Goner "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/urllib",target:"_blank",rel:"noopener noreferrer"}},[t._v("urllib"),e("OutboundLink")],1),t._v(" to send http requests to Gone Web programs, and the traceId will be automatically passed between services. Additionally, using the built-in "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/grpc",target:"_blank",rel:"noopener noreferrer"}},[t._v("grpc"),e("OutboundLink")],1),t._v(" to implement gRPC calls will also automatically pass the traceId. More RPC call support will be provided in the future.")])]),t._v(" "),e("h2",{attrs:{id:"multi-language-support"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#multi-language-support"}},[t._v("#")]),t._v(" Multi-language Support")]),t._v(" "),e("p",[t._v("To pass the traceId in an HTTP request, a special header "),e("code",[t._v("X-Trace-ID")]),t._v(' is added to carry the traceId. Therefore, if multiple programming languages are used, as long as the different services follow the rule of "attaching '),e("code",[t._v("X-Trace-ID")]),t._v(" when making requests on the client side and parsing "),e("code",[t._v("X-Trace-ID")]),t._v(' when processing requests on the server side," traceId can be seamlessly passed across services developed in different languages.'),e("br"),t._v("\nWe plan to develop packages in other languages to seamlessly integrate with other languages in the future.")])])}),[],!1,null,null,null);e.default=n.exports}}]); \ No newline at end of file diff --git a/assets/js/40.db3cd4e1.js b/assets/js/39.b0d9a82b.js similarity index 99% rename from assets/js/40.db3cd4e1.js rename to assets/js/39.b0d9a82b.js index e7c4a28..edd3d61 100644 --- a/assets/js/40.db3cd4e1.js +++ b/assets/js/39.b0d9a82b.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[40],{333:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"unit-testing"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#unit-testing"}},[t._v("#")]),t._v(" Unit Testing")]),t._v(" "),n("p",[t._v("In a dependency injection framework, how to conduct unit tests involves injecting dependencies before testing. If the test content is closely related to the injected content, consider mocking the related content. One way is to manually bury the mocked Goner into the system, and another way is to replace an already buried Goner using "),n("code",[t._v("cemetery.ReplaceBury")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"assume-our-written-goner-is-as-follows"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#assume-our-written-goner-is-as-follows"}},[t._v("#")]),t._v(" Assume Our Written Goner is as Follows")]),t._v(" "),n("p",[t._v("Filename: goner.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameA "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-a"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameB "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-b"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameB\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Point "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tX "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-x"`')]),t._v("\n\tY "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-y,default=200"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Line "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tA "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n\tB "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-b"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"we-can-write-a-test-file-as-follows"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#we-can-write-a-test-file-as-follows"}},[t._v("#")]),t._v(" We Can Write a Test File as Follows:")]),t._v(" "),n("p",[t._v("Filename: goner_test.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_Line")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"ReplaceBury"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tMock "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReplaceBury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mock")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"using-gomock-for-mock-testing"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#using-gomock-for-mock-testing"}},[t._v("#")]),t._v(" Using gomock for Mock Testing")]),t._v(" "),n("p",[t._v("To decouple, we recommend using interfaces for injection; another reason for recommending interface injection is that Go provides an interface-based mock solution, allowing all dependencies to be mocked. However, mock implementations generated by "),n("code",[t._v("mockgen")]),t._v(" are not "),n("code",[t._v("Goner")]),t._v(" and thus cannot be buried or injected; for this, we provide a solution in our utility tools.")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone mock "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n")])])]),n("p",[t._v("The "),n("code",[t._v("gone mock")]),t._v(" command adds the "),n("code",[t._v("gone.Flag")]),t._v(" to a mock object generated by "),n("code",[t._v("mockgen")]),t._v(", making it a Goner that can enter the Cemetery.")]),t._v(" "),n("p",[t._v("Typically, add a "),n("code",[t._v("//go:generate")]),t._v(" comment on the interface that needs mocking, and let the generation process complete automatically with the "),n("code",[t._v("go generate ./...")]),t._v(" command. Here's an example:")]),t._v(" "),n("p",[t._v("Filename: i_point.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE | gone mock -o mock/$GOFILE"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IPoint "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The above "),n("code",[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE | gone mock -o mock/$GOFILE"')]),t._v(" command first generates a mock object for the interface using mockgen, then pipes it to "),n("code",[t._v("gone mock")]),t._v(" to add the "),n("code",[t._v("gone.Flag")]),t._v(" tag.")]),t._v(" "),n("blockquote",[n("p",[t._v("Note: Keep the versions of the mockgen tool and gomock package consistent;\nRun the following code to install the latest versions:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go get github.com/golang/mock/gomock\ngo get github.com/golang/mock/mockgen\n")])])]),n("p",[t._v("The gone utility tool needs to be installed; installation reference "),n("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone Utility Tools"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("p",[t._v("Now, let's give it a try. Create an empty directory, enter it, create the "),n("code",[t._v("i_test.go")]),t._v(" file as mentioned, and run the command in the current")]),t._v(" "),n("p",[t._v("directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go generate ./"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\n")])])]),n("p",[t._v("You will see that after running the command, the file "),n("code",[t._v("mock/i_point.go")]),t._v(" is generated.")]),t._v(" "),n("p",[t._v("Next, create a file named "),n("code",[t._v("origin_point.go")]),t._v(" with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" originPoint "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewOriginPoint")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Create another file named "),n("code",[t._v("distance_calculator.go")]),t._v(" with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"math"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" distanceCalculator "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\toriginPoint IPoint "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" CalculateDistance "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("FromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" y "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\toriginX"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" originY "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sqrt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originX"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("y"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originY"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The "),n("code",[t._v("distanceCalculator")]),t._v("'s business is to calculate the distance from the point "),n("code",[t._v("(x,y int)")]),t._v(" to "),n("code",[t._v("originPoint")]),t._v(", where "),n("code",[t._v("originPoint")]),t._v(" is injected. Now let's write the test function for "),n("code",[t._v("CalculateDistanceFromOrigin")]),t._v(" as follows:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example/test/mock"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/golang/mock/gomock"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_distanceCalculator_CalculateDistanceFromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Create a mock controller")]),t._v("\n\tcontroller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gomock"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewController")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" controller"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Finish")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tdistance "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" distance"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Create a mock object")]),t._v("\n\t\tpoint "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" mock"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewMockIPoint")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("controller"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury the mock object in the Cemetery")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The object under test also needs to be buried in the Cemetery")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[39],{326:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"unit-testing"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#unit-testing"}},[t._v("#")]),t._v(" Unit Testing")]),t._v(" "),n("p",[t._v("In a dependency injection framework, how to conduct unit tests involves injecting dependencies before testing. If the test content is closely related to the injected content, consider mocking the related content. One way is to manually bury the mocked Goner into the system, and another way is to replace an already buried Goner using "),n("code",[t._v("cemetery.ReplaceBury")]),t._v(".")]),t._v(" "),n("h2",{attrs:{id:"assume-our-written-goner-is-as-follows"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#assume-our-written-goner-is-as-follows"}},[t._v("#")]),t._v(" Assume Our Written Goner is as Follows")]),t._v(" "),n("p",[t._v("Filename: goner.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameA "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-a"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameB "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-b"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameB\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Point "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tX "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-x"`')]),t._v("\n\tY "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-y,default=200"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Line "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tA "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n\tB "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-b"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"we-can-write-a-test-file-as-follows"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#we-can-write-a-test-file-as-follows"}},[t._v("#")]),t._v(" We Can Write a Test File as Follows:")]),t._v(" "),n("p",[t._v("Filename: goner_test.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_Line")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"ReplaceBury"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tMock "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReplaceBury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mock")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"using-gomock-for-mock-testing"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#using-gomock-for-mock-testing"}},[t._v("#")]),t._v(" Using gomock for Mock Testing")]),t._v(" "),n("p",[t._v("To decouple, we recommend using interfaces for injection; another reason for recommending interface injection is that Go provides an interface-based mock solution, allowing all dependencies to be mocked. However, mock implementations generated by "),n("code",[t._v("mockgen")]),t._v(" are not "),n("code",[t._v("Goner")]),t._v(" and thus cannot be buried or injected; for this, we provide a solution in our utility tools.")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone mock "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n")])])]),n("p",[t._v("The "),n("code",[t._v("gone mock")]),t._v(" command adds the "),n("code",[t._v("gone.Flag")]),t._v(" to a mock object generated by "),n("code",[t._v("mockgen")]),t._v(", making it a Goner that can enter the Cemetery.")]),t._v(" "),n("p",[t._v("Typically, add a "),n("code",[t._v("//go:generate")]),t._v(" comment on the interface that needs mocking, and let the generation process complete automatically with the "),n("code",[t._v("go generate ./...")]),t._v(" command. Here's an example:")]),t._v(" "),n("p",[t._v("Filename: i_point.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE | gone mock -o mock/$GOFILE"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IPoint "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The above "),n("code",[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE | gone mock -o mock/$GOFILE"')]),t._v(" command first generates a mock object for the interface using mockgen, then pipes it to "),n("code",[t._v("gone mock")]),t._v(" to add the "),n("code",[t._v("gone.Flag")]),t._v(" tag.")]),t._v(" "),n("blockquote",[n("p",[t._v("Note: Keep the versions of the mockgen tool and gomock package consistent;\nRun the following code to install the latest versions:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go get github.com/golang/mock/gomock\ngo get github.com/golang/mock/mockgen\n")])])]),n("p",[t._v("The gone utility tool needs to be installed; installation reference "),n("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone Utility Tools"),n("OutboundLink")],1),t._v(".")])]),t._v(" "),n("p",[t._v("Now, let's give it a try. Create an empty directory, enter it, create the "),n("code",[t._v("i_test.go")]),t._v(" file as mentioned, and run the command in the current")]),t._v(" "),n("p",[t._v("directory:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go generate ./"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\n")])])]),n("p",[t._v("You will see that after running the command, the file "),n("code",[t._v("mock/i_point.go")]),t._v(" is generated.")]),t._v(" "),n("p",[t._v("Next, create a file named "),n("code",[t._v("origin_point.go")]),t._v(" with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" originPoint "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewOriginPoint")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("Create another file named "),n("code",[t._v("distance_calculator.go")]),t._v(" with the following content:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"math"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" distanceCalculator "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\toriginPoint IPoint "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" CalculateDistance "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("FromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" y "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\toriginX"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" originY "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sqrt")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originX"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" math"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("y"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originY"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The "),n("code",[t._v("distanceCalculator")]),t._v("'s business is to calculate the distance from the point "),n("code",[t._v("(x,y int)")]),t._v(" to "),n("code",[t._v("originPoint")]),t._v(", where "),n("code",[t._v("originPoint")]),t._v(" is injected. Now let's write the test function for "),n("code",[t._v("CalculateDistanceFromOrigin")]),t._v(" as follows:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example/test/mock"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/golang/mock/gomock"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_distanceCalculator_CalculateDistanceFromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Create a mock controller")]),t._v("\n\tcontroller "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gomock"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewController")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" controller"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Finish")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tdistance "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" distance"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Create a mock object")]),t._v("\n\t\tpoint "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" mock"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewMockIPoint")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("controller"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury the mock object in the Cemetery")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The object under test also needs to be buried in the Cemetery")]),t._v("\n\t\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/41.bb836d0a.js b/assets/js/40.6cdb457d.js similarity index 99% rename from assets/js/41.bb836d0a.js rename to assets/js/40.6cdb457d.js index ecd3fc7..0a70933 100644 --- a/assets/js/41.bb836d0a.js +++ b/assets/js/40.6cdb457d.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[41],{326:function(t,s,a){"use strict";a.r(s);var e=a(10),n=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"supporting-database-operations-with-built-in-goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#supporting-database-operations-with-built-in-goners"}},[t._v("#")]),t._v(" Supporting Database Operations with Built-in Goners")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#example-with-mysql"}},[t._v("Example with MySQL")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-prepare-mysql-service"}},[t._v("1. Prepare MySQL Service")])]),s("li",[s("a",{attrs:{href:"#_2-create-module-and-configuration"}},[t._v("2. Create Module and Configuration")])]),s("li",[s("a",{attrs:{href:"#_3-write-code"}},[t._v("3. Write Code")])]),s("li",[s("a",{attrs:{href:"#_4-run-the-code"}},[t._v("4. Run the Code")])])])]),s("li",[s("a",{attrs:{href:"#points-to-note"}},[t._v("Points to Note")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[t._v("1. github.com/gone-io/gone/goner is a wrapper for xorm.io/xorm")])]),s("li",[s("a",{attrs:{href:"#_2-injection-via-gone-xorm-gonerid"}},[t._v("2. Injection via gone-xorm GonerId")])]),s("li",[s("a",{attrs:{href:"#_3-pay-attention-to-imported-packages"}},[t._v("3. Pay Attention to Imported Packages")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"example-with-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#example-with-mysql"}},[t._v("#")]),t._v(" Example with MySQL")]),t._v(" "),s("h3",{attrs:{id:"_1-prepare-mysql-service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-prepare-mysql-service"}},[t._v("#")]),t._v(" 1. Prepare MySQL Service")]),t._v(" "),s("p",[t._v("You can quickly set up a MySQL service using Docker Compose. Create a "),s("code",[t._v("docker-compose.yaml")]),t._v(" file with the following content:")]),t._v(" "),s("div",{staticClass:"language-yaml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-yaml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3.5"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("services")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("mysql")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("image")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8.3")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("restart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" unless"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v("stopped\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("environment")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_ROOT_PASSWORD")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("123456")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_DATABASE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" demo\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("volumes")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" ./data/mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("/var/lib/mysql\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("ports")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3306:3306"')]),t._v("\n")])])]),s("p",[t._v("Run the following command in the terminal to start the MySQL service:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker-compose")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" docker-compose.yaml up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-create-module-and-configuration"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-create-module-and-configuration"}},[t._v("#")]),t._v(" 2. Create Module and Configuration")]),t._v(" "),s("p",[t._v("Execute the following commands to set up the project structure and configuration:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" use-rdb\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Initialize Go module")]),t._v("\ngo mod init use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Create database configuration")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("cat")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),t._v(" config/default.properties"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("eof\n\n# Use MySQL database\ndatabase.driver-name=mysql\n\n# Data Source Name (DSN)\ndatabase.dsn=root:123456@tcp(localhost:3306)/demo?charset=utf8mb4&loc=Local\neof")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-write-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-write-code"}},[t._v("#")]),t._v(" 3. Write Code")]),t._v(" "),s("p",[t._v("Create a file named "),s("code",[t._v("main.go")]),t._v(" with the following code:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ⚠️ Import MySQL driver explicitly")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ⚠️ Import xorm package from gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v("\n\tData "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SqlExecutor "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdb xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Synchronize table structure")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sync")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("new")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tdemo "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello gone"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Insert data")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Insert")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\tDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. "')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo: %v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" list "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Demo\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Read data")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Find")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo records:%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury xorm-related Goners into the cemetery using goner.XormPriest")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Execute after the Gone application starts")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\te SqlExecutor "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-run-the-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-run-the-code"}},[t._v("#")]),t._v(" 4. Run the Code")]),t._v(" "),s("p",[t._v("Run the following commands:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("The running result is as follows:")]),t._v(" "),s("div",{staticClass:"language-log extra-class"},[s("div",{staticClass:"highlight-lines"},[s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("br")]),s("pre",{pre:!0,attrs:{class:"language-log"}},[s("code",[s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.767")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("heaven\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("cemetery\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" local\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/default.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/example/use-rdb/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("xorm"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("engine\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive main"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("SqlExecutor\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.769")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("90")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.783")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/cemetery.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("307")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("\ndemo"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v("\ndemo records"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v(" The most Spring programmer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("friendly Golang framework"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" dependency injection"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" integrates Web"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n")])])]),s("h2",{attrs:{id:"points-to-note"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#points-to-note"}},[t._v("#")]),t._v(" Points to Note")]),t._v(" "),s("h3",{attrs:{id:"_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[t._v("#")]),t._v(" 1. "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" is a wrapper for "),s("code",[t._v("xorm.io/xorm")])]),t._v(" "),s("p",[t._v("The wrapper logic is roughly as follows: "),s("code",[t._v("xorm")]),t._v(" is wrapped as an "),s("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html#%F0%9F%98%87angel-%E5%A4%A9%E4%BD%BF",target:"_blank",rel:"noopener noreferrer"}},[t._v("Angel Goner"),s("OutboundLink")],1),t._v("; data connection is established in the "),s("code",[t._v("Start")]),t._v(" method, and the database connection is closed in the "),s("code",[t._v("Stop")]),t._v(" method. When using it, there is no need to import the "),s("code",[t._v("xorm.io/xorm")]),t._v(" package directly, only "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" needs to be imported. The wrapper relies on "),s("code",[t._v("github.com/gone-io/gone/tree/main/goner/config")]),t._v(" to implement configuration. The burial of configuration-related Goners is automatically completed in the "),s("code",[t._v("goner.XormPriest")]),t._v(" function.\nSupported configuration items in the configuration file:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("database.driver-name")]),t._v(": Name of the driver, supports mainstream databases like MySQL, PostgreSQL, etc. For details, refer to the "),s("a",{attrs:{href:"https://xorm.io/docs/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1)]),t._v(" "),s("li",[s("code",[t._v("database.dsn")]),t._v(": Data source name, refer to the "),s("a",{attrs:{href:"https://xorm.io/docs/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1)]),t._v(" "),s("li",[s("code",[t._v("database.max-idle-count")]),t._v(": Maximum number of idle connections in the connection pool")]),t._v(" "),s("li",[s("code",[t._v("database.max-open")]),t._v(": Maximum number of connections in the connection pool")]),t._v(" "),s("li",[s("code",[t._v("database.max-lifetime")]),t._v(": Maximum lifetime of a connection")]),t._v(" "),s("li",[s("code",[t._v("database.showSql")]),t._v(": Whether to print executed SQL statements")])]),t._v(" "),s("h3",{attrs:{id:"_2-injection-via-gone-xorm-gonerid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-injection-via-gone-xorm-gonerid"}},[t._v("#")]),t._v(" 2. Injection via "),s("code",[t._v("gone-xorm")]),t._v(" "),s("strong",[t._v("GonerId")])]),t._v(" "),s("p",[t._v("In the Goner where the xorm engine is needed, use the "),s("code",[t._v('gone:"gone-xorm"')]),t._v(" tag to inject "),s("code",[t._v("xorm.Engine")]),t._v(", properties can be named or anonymous:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named")]),t._v("\n db xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous")]),t._v("\n xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("code",[t._v("xorm.Engine")]),t._v(' "inherits" the '),s("code",[t._v("xorm.io/xorm.EngineInterface")]),t._v(" interface, so "),s("code",[t._v("xorm.Engine")]),t._v(" supports all methods in the "),s("a",{attrs:{href:"https://xorm.io/docs/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1),t._v(".")]),t._v(" "),s("h3",{attrs:{id:"_3-pay-attention-to-imported-packages"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-pay-attention-to-imported-packages"}},[t._v("#")]),t._v(" 3. Pay Attention to Imported Packages")]),t._v(" "),s("ul",[s("li",[t._v("Import the database driver, which varies for different databases:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("ul",[s("li",[t._v("Import the xorm package as "),s("code",[t._v("github.com/gone-io/gone/goner/xorm")]),t._v(", not "),s("code",[t._v("xorm.io/xorm")]),t._v(":")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #📢⚠️Special attention needed"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" the xorm "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" wrapped by gone\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=n.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[40],{331:function(t,s,a){"use strict";a.r(s);var e=a(10),n=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"supporting-database-operations-with-built-in-goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#supporting-database-operations-with-built-in-goners"}},[t._v("#")]),t._v(" Supporting Database Operations with Built-in Goners")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#example-with-mysql"}},[t._v("Example with MySQL")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-prepare-mysql-service"}},[t._v("1. Prepare MySQL Service")])]),s("li",[s("a",{attrs:{href:"#_2-create-module-and-configuration"}},[t._v("2. Create Module and Configuration")])]),s("li",[s("a",{attrs:{href:"#_3-write-code"}},[t._v("3. Write Code")])]),s("li",[s("a",{attrs:{href:"#_4-run-the-code"}},[t._v("4. Run the Code")])])])]),s("li",[s("a",{attrs:{href:"#points-to-note"}},[t._v("Points to Note")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[t._v("1. github.com/gone-io/gone/goner is a wrapper for xorm.io/xorm")])]),s("li",[s("a",{attrs:{href:"#_2-injection-via-gone-xorm-gonerid"}},[t._v("2. Injection via gone-xorm GonerId")])]),s("li",[s("a",{attrs:{href:"#_3-pay-attention-to-imported-packages"}},[t._v("3. Pay Attention to Imported Packages")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"example-with-mysql"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#example-with-mysql"}},[t._v("#")]),t._v(" Example with MySQL")]),t._v(" "),s("h3",{attrs:{id:"_1-prepare-mysql-service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-prepare-mysql-service"}},[t._v("#")]),t._v(" 1. Prepare MySQL Service")]),t._v(" "),s("p",[t._v("You can quickly set up a MySQL service using Docker Compose. Create a "),s("code",[t._v("docker-compose.yaml")]),t._v(" file with the following content:")]),t._v(" "),s("div",{staticClass:"language-yaml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-yaml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3.5"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("services")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("mysql")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("image")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8.3")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("restart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" unless"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v("stopped\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("environment")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_ROOT_PASSWORD")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("123456")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_DATABASE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" demo\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("volumes")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" ./data/mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("/var/lib/mysql\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("ports")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3306:3306"')]),t._v("\n")])])]),s("p",[t._v("Run the following command in the terminal to start the MySQL service:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker-compose")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" docker-compose.yaml up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-create-module-and-configuration"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-create-module-and-configuration"}},[t._v("#")]),t._v(" 2. Create Module and Configuration")]),t._v(" "),s("p",[t._v("Execute the following commands to set up the project structure and configuration:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" use-rdb\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Initialize Go module")]),t._v("\ngo mod init use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Create database configuration")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("cat")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),t._v(" config/default.properties"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("eof\n\n# Use MySQL database\ndatabase.driver-name=mysql\n\n# Data Source Name (DSN)\ndatabase.dsn=root:123456@tcp(localhost:3306)/demo?charset=utf8mb4&loc=Local\neof")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-write-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-write-code"}},[t._v("#")]),t._v(" 3. Write Code")]),t._v(" "),s("p",[t._v("Create a file named "),s("code",[t._v("main.go")]),t._v(" with the following code:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ⚠️ Import MySQL driver explicitly")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ⚠️ Import xorm package from gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v("\n\tData "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SqlExecutor "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdb xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Synchronize table structure")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sync")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("new")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tdemo "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello gone"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Insert data")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Insert")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\tDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. "')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo: %v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" list "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Demo\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Read data")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Find")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo records:%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Bury xorm-related Goners into the cemetery using goner.XormPriest")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Execute after the Gone application starts")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\te SqlExecutor "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-run-the-code"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-run-the-code"}},[t._v("#")]),t._v(" 4. Run the Code")]),t._v(" "),s("p",[t._v("Run the following commands:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("The running result is as follows:")]),t._v(" "),s("div",{staticClass:"language-log extra-class"},[s("div",{staticClass:"highlight-lines"},[s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("br")]),s("pre",{pre:!0,attrs:{class:"language-log"}},[s("code",[s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.767")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("heaven\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("cemetery\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" local\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/default.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/example/use-rdb/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("xorm"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("engine\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive main"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("SqlExecutor\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.769")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("90")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.783")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/cemetery.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("307")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("\ndemo"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v("\ndemo records"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v(" The most Spring programmer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("friendly Golang framework"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" dependency injection"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" integrates Web"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n")])])]),s("h2",{attrs:{id:"points-to-note"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#points-to-note"}},[t._v("#")]),t._v(" Points to Note")]),t._v(" "),s("h3",{attrs:{id:"_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"}},[t._v("#")]),t._v(" 1. "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" is a wrapper for "),s("code",[t._v("xorm.io/xorm")])]),t._v(" "),s("p",[t._v("The wrapper logic is roughly as follows: "),s("code",[t._v("xorm")]),t._v(" is wrapped as an "),s("a",{attrs:{href:"https://goner.fun/en/guide/core-concept.html#%F0%9F%98%87angel-%E5%A4%A9%E4%BD%BF",target:"_blank",rel:"noopener noreferrer"}},[t._v("Angel Goner"),s("OutboundLink")],1),t._v("; data connection is established in the "),s("code",[t._v("Start")]),t._v(" method, and the database connection is closed in the "),s("code",[t._v("Stop")]),t._v(" method. When using it, there is no need to import the "),s("code",[t._v("xorm.io/xorm")]),t._v(" package directly, only "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" needs to be imported. The wrapper relies on "),s("code",[t._v("github.com/gone-io/gone/tree/main/goner/config")]),t._v(" to implement configuration. The burial of configuration-related Goners is automatically completed in the "),s("code",[t._v("goner.XormPriest")]),t._v(" function.\nSupported configuration items in the configuration file:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("database.driver-name")]),t._v(": Name of the driver, supports mainstream databases like MySQL, PostgreSQL, etc. For details, refer to the "),s("a",{attrs:{href:"https://xorm.io/docs/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1)]),t._v(" "),s("li",[s("code",[t._v("database.dsn")]),t._v(": Data source name, refer to the "),s("a",{attrs:{href:"https://xorm.io/docs/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1)]),t._v(" "),s("li",[s("code",[t._v("database.max-idle-count")]),t._v(": Maximum number of idle connections in the connection pool")]),t._v(" "),s("li",[s("code",[t._v("database.max-open")]),t._v(": Maximum number of connections in the connection pool")]),t._v(" "),s("li",[s("code",[t._v("database.max-lifetime")]),t._v(": Maximum lifetime of a connection")]),t._v(" "),s("li",[s("code",[t._v("database.showSql")]),t._v(": Whether to print executed SQL statements")])]),t._v(" "),s("h3",{attrs:{id:"_2-injection-via-gone-xorm-gonerid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-injection-via-gone-xorm-gonerid"}},[t._v("#")]),t._v(" 2. Injection via "),s("code",[t._v("gone-xorm")]),t._v(" "),s("strong",[t._v("GonerId")])]),t._v(" "),s("p",[t._v("In the Goner where the xorm engine is needed, use the "),s("code",[t._v('gone:"gone-xorm"')]),t._v(" tag to inject "),s("code",[t._v("xorm.Engine")]),t._v(", properties can be named or anonymous:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Named")]),t._v("\n db xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Or")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Anonymous")]),t._v("\n xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("code",[t._v("xorm.Engine")]),t._v(' "inherits" the '),s("code",[t._v("xorm.io/xorm.EngineInterface")]),t._v(" interface, so "),s("code",[t._v("xorm.Engine")]),t._v(" supports all methods in the "),s("a",{attrs:{href:"https://xorm.io/docs/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm documentation"),s("OutboundLink")],1),t._v(".")]),t._v(" "),s("h3",{attrs:{id:"_3-pay-attention-to-imported-packages"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-pay-attention-to-imported-packages"}},[t._v("#")]),t._v(" 3. Pay Attention to Imported Packages")]),t._v(" "),s("ul",[s("li",[t._v("Import the database driver, which varies for different databases:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("ul",[s("li",[t._v("Import the xorm package as "),s("code",[t._v("github.com/gone-io/gone/goner/xorm")]),t._v(", not "),s("code",[t._v("xorm.io/xorm")]),t._v(":")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #📢⚠️Special attention needed"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" the xorm "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" wrapped by gone\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=n.exports}}]); \ No newline at end of file diff --git a/assets/js/42.5ca17854.js b/assets/js/41.c24f5d64.js similarity index 95% rename from assets/js/42.5ca17854.js rename to assets/js/41.c24f5d64.js index 2066d56..5a31cb9 100644 --- a/assets/js/42.5ca17854.js +++ b/assets/js/41.c24f5d64.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[42],{327:function(t,e,r){"use strict";r.r(e);var a=r(10),s=Object(a.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"quick-start"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#quick-start"}},[t._v("#")]),t._v(" Quick Start")]),t._v(" "),e("h2",{attrs:{id:"create-a-simple-web-project"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#create-a-simple-web-project"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/quick-start/simple.html"}},[t._v("Create a Simple Web Project")])],1),t._v(" "),e("h2",{attrs:{id:"create-a-production-ready-project-using-mysql"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#create-a-production-ready-project-using-mysql"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/quick-start/simple.html"}},[t._v("Create a Production-Ready Project Using MySQL")])],1),t._v(" "),e("h2",{attrs:{id:"more-examples"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#more-examples"}},[t._v("#")]),t._v(" "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example",target:"_blank",rel:"noopener noreferrer"}},[t._v("More Examples"),e("OutboundLink")],1)])])}),[],!1,null,null,null);e.default=s.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[41],{325:function(t,e,r){"use strict";r.r(e);var a=r(10),s=Object(a.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"quick-start"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#quick-start"}},[t._v("#")]),t._v(" Quick Start")]),t._v(" "),e("h2",{attrs:{id:"create-a-simple-web-project"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#create-a-simple-web-project"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/quick-start/simple.html"}},[t._v("Create a Simple Web Project")])],1),t._v(" "),e("h2",{attrs:{id:"create-a-production-ready-project-using-mysql"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#create-a-production-ready-project-using-mysql"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/quick-start/simple.html"}},[t._v("Create a Production-Ready Project Using MySQL")])],1),t._v(" "),e("h2",{attrs:{id:"more-examples"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#more-examples"}},[t._v("#")]),t._v(" "),e("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example",target:"_blank",rel:"noopener noreferrer"}},[t._v("More Examples"),e("OutboundLink")],1)])])}),[],!1,null,null,null);e.default=s.exports}}]); \ No newline at end of file diff --git a/assets/js/43.3ee054e7.js b/assets/js/42.c4e3f60f.js similarity index 99% rename from assets/js/43.3ee054e7.js rename to assets/js/42.c4e3f60f.js index 4ac7785..7c035aa 100644 --- a/assets/js/43.3ee054e7.js +++ b/assets/js/42.c4e3f60f.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[43],{329:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"creating-a-simple-web-project"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#creating-a-simple-web-project"}},[t._v("#")]),t._v(" Creating a Simple Web Project")]),t._v(" "),n("h2",{attrs:{id:"installing-the-gone-utility-tool"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#installing-the-gone-utility-tool"}},[t._v("#")]),t._v(" Installing the Gone Utility Tool")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),n("blockquote",[n("p",[t._v("After installation, you can use the gone command:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n")])])]),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("\t➜ demo gone "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n\tNAME:\n\tgone - A new cli application\n\n\tUSAGE:\n\tgone "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("global options"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("command options"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("arguments"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v("."),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n\tDESCRIPTION:\n\tgenerate gone code or generate gone app\n\n\tCOMMANDS:\n\tpriest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${scanPackageDir}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${pkgName}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${funcName}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outputFilePath}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-w"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\tmock "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n\tcreate "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-t "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${template}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-m "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${modName}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${appName}")]),t._v("\n\thelp, h Shows a list of commands or "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" one "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v("\n\n\tGLOBAL OPTIONS:\n\t--help, "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v(" show "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("default: "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("false")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("p",[t._v("Supported functionalities:")]),t._v(" "),n("ol",[n("li",[t._v("create: Create a Gone app, currently only supports creating web apps.")]),t._v(" "),n("li",[t._v("priest: Automatically generate the "),n("strong",[t._v("Priest")]),t._v(" function for the project. "),n("RouterLink",{attrs:{to:"/"}},[t._v("Learn More")])],1),t._v(" "),n("li",[t._v("Generating mock code for testing.")])])]),t._v(" "),n("h2",{attrs:{id:"creating-a-web-project-and-running-the-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#creating-a-web-project-and-running-the-code"}},[t._v("#")]),t._v(" Creating a Web Project and Running the Code")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Create a project named web-app")]),t._v("\ngone create web-app\n"),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-app\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),n("h2",{attrs:{id:"project-structure"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#project-structure"}},[t._v("#")]),t._v(" Project Structure")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go // Startup file, containing the main function.\n├── config // Project configuration directory, supporting "),n("span",{pre:!0,attrs:{class:"token variable"}},[n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("`")]),t._v(".properties"),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("`")])]),t._v(" files.\n│ ├── default.properties\n│ ├── dev.properties\n│ ├── local.properties\n│ └── prod.properties\n├── docker-compose.yaml\n├── go.mod\n├── internal\n│ ├── controller // Controller directory, "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" defining routes "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" files.\n│ │ └── demo_ctr.go\n│ ├── interface\n│ │ ├── domain // Directory "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" domain objects.\n│ │ │ ├── demo.go\n│ │ │ └── user.go\n│ │ └── "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" // Directory "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" interface definitions.\n│ │ └── i_demo.go\n│ ├── master.go\n│ ├── middleware // Middleware directory, "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" defining web middleware.\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module // Module directory, each subdirectory implements a module's functionality, typically defined "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" internal/interface/service/.\n│ │ └── demo\n│ │ ├── demo_svc.go\n│ │ └── error.go\n│ ├── pkg\n│ │ └── utils\n│ │ └── error.go\n│ └── router // Define routers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" this directory.\n│ ├── auth_router.go\n│ └── pub_router.go\n└── tests\n └── api\n └── demo.http\n")])])]),n("h2",{attrs:{id:"router"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#router"}},[t._v("#")]),t._v(" Router")]),t._v(" "),n("p",[t._v("In the directory "),n("code",[t._v("internal/router")]),t._v(", two "),n("code",[t._v("gin.IRouter")]),t._v("s are implemented:")]),t._v(" "),n("ul",[n("li",[t._v("pubRouter: Public router, endpoints mounted under this router can be accessed without authorization.")]),t._v(" "),n("li",[t._v("authRouter: Authenticated router, endpoints mounted under this router require authorization.")])]),t._v(" "),n("p",[t._v("Let's analyze the code in "),n("code",[t._v("internal/router/pub_router.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" router\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/middleware"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" IdRouterPub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"router-pub"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPubRouter")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("pubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" IdRouterPub\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" pubRouter "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter\n\troot gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v("\n\tpub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("middleware"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PubMiddleware "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("pubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("root"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/api"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" r"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pub"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Next"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("ol",[n("li",[t._v("For routers, they need to implement the methods defined in "),n("code",[t._v("gone.IRouter")]),t._v(".")]),t._v(" "),n("li",[t._v("The struct "),n("code",[t._v("pubRouter")]),t._v(" embeds an "),n("code",[t._v("gone.IRouter")]),t._v(", which means it directly implements the "),n("code",[t._v("gone.IRouter")]),t._v(" interface. It is only assigned a value in "),n("code",[t._v("AfterRevive()")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v('r.IRouter = r.root.Group("/api", r.pub.Next)')]),t._v(" means the current router is a subrouter under the root router at "),n("code",[t._v("/api")]),t._v(", with an additional middleware "),n("code",[t._v("r.pub.Next")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v("root gone.IRouter")]),t._v(" is a framework-level Goner provided by the "),n("code",[t._v("github.com/gone-io/gone/goner/gin")]),t._v(" package, with a named injection "),n("code",[t._v("gone-gin-router")]),t._v(".")])]),t._v(" "),n("h2",{attrs:{id:"controller"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#controller"}},[t._v("#")]),t._v(" Controller")]),t._v(" "),n("p",[t._v("Below is the definition of the Controller interface:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Controller interface, implemented by business code to mount and handle routes")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// For usage, refer to [Sample Code](https://gitlab.openviewtech.com/gone/gone-example/-/tree/master/gone-app)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Controller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Mount Route mounting interface, this function will be called before service startup, and the implementation of this function should usually return `nil`")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" MountError\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("To write HTTP interfaces, we need to implement the "),n("code",[t._v("Controller")]),t._v(" interface and mount the interface routes in the "),n("code",[t._v("Mount")]),t._v(" method, as shown in the code in "),n("code",[t._v("internal/controller/demo_ctr.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" controller\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/service"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/pkg/utils"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoController")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Dependency injection of service")]),t._v("\n\n\tauthRouter gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-auth"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Router injection")]),t._v("\n\tpubRouter gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-pub"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Router injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tauthRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/error"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/echo"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("echo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("showDemo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tEcho "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"echo"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" req Req\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" ctx"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bind")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("req"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" utils"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ParameterParseError"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("req"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Echo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"service"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#service"}},[t._v("#")]),t._v(" Service")]),t._v(" "),n("p",[t._v("As per the standard, we require service interfaces to be defined in the "),n("code",[t._v("internal/interface/service")]),t._v(" directory. File names should begin with "),n("code",[t._v("i_")]),t._v(", and interface types should start with "),n("code",[t._v("I")]),t._v(", for example:")]),t._v(" "),n("p",[t._v("File: i_demo.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" service\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IDemo "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("DemoEntity"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The logic implementation of services is placed in the "),n("code",[t._v("internal/module")]),t._v(" directory, organized into modules.")]),t._v(" "),n("h2",{attrs:{id:"use-database"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#use-database"}},[t._v("#")]),t._v(" "),n("a",{attrs:{href:"https://goner.fun/guide/xorm.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Use Database"),n("OutboundLink")],1)])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[42],{328:function(t,n,a){"use strict";a.r(n);var s=a(10),e=Object(s.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"creating-a-simple-web-project"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#creating-a-simple-web-project"}},[t._v("#")]),t._v(" Creating a Simple Web Project")]),t._v(" "),n("h2",{attrs:{id:"installing-the-gone-utility-tool"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#installing-the-gone-utility-tool"}},[t._v("#")]),t._v(" Installing the Gone Utility Tool")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("go "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),n("blockquote",[n("p",[t._v("After installation, you can use the gone command:")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("gone "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n")])])]),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("\t➜ demo gone "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n\tNAME:\n\tgone - A new cli application\n\n\tUSAGE:\n\tgone "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("global options"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("command options"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("arguments"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v("."),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n\tDESCRIPTION:\n\tgenerate gone code or generate gone app\n\n\tCOMMANDS:\n\tpriest "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${scanPackageDir}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${pkgName}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${funcName}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outputFilePath}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-w"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\tmock "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n\tcreate "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-t "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${template}")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-m "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${modName}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${appName}")]),t._v("\n\thelp, h Shows a list of commands or "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" one "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v("\n\n\tGLOBAL OPTIONS:\n\t--help, "),n("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v(" show "),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("default: "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("false")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("p",[t._v("Supported functionalities:")]),t._v(" "),n("ol",[n("li",[t._v("create: Create a Gone app, currently only supports creating web apps.")]),t._v(" "),n("li",[t._v("priest: Automatically generate the "),n("strong",[t._v("Priest")]),t._v(" function for the project. "),n("RouterLink",{attrs:{to:"/"}},[t._v("Learn More")])],1),t._v(" "),n("li",[t._v("Generating mock code for testing.")])])]),t._v(" "),n("h2",{attrs:{id:"creating-a-web-project-and-running-the-code"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#creating-a-web-project-and-running-the-code"}},[t._v("#")]),t._v(" Creating a Web Project and Running the Code")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# Create a project named web-app")]),t._v("\ngone create web-app\n"),n("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-app\n"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),n("h2",{attrs:{id:"project-structure"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#project-structure"}},[t._v("#")]),t._v(" Project Structure")]),t._v(" "),n("div",{staticClass:"language-bash extra-class"},[n("pre",{pre:!0,attrs:{class:"language-bash"}},[n("code",[t._v("├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go // Startup file, containing the main function.\n├── config // Project configuration directory, supporting "),n("span",{pre:!0,attrs:{class:"token variable"}},[n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("`")]),t._v(".properties"),n("span",{pre:!0,attrs:{class:"token variable"}},[t._v("`")])]),t._v(" files.\n│ ├── default.properties\n│ ├── dev.properties\n│ ├── local.properties\n│ └── prod.properties\n├── docker-compose.yaml\n├── go.mod\n├── internal\n│ ├── controller // Controller directory, "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" defining routes "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" files.\n│ │ └── demo_ctr.go\n│ ├── interface\n│ │ ├── domain // Directory "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" domain objects.\n│ │ │ ├── demo.go\n│ │ │ └── user.go\n│ │ └── "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" // Directory "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v(" interface definitions.\n│ │ └── i_demo.go\n│ ├── master.go\n│ ├── middleware // Middleware directory, "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" defining web middleware.\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module // Module directory, each subdirectory implements a module's functionality, typically defined "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" internal/interface/service/.\n│ │ └── demo\n│ │ ├── demo_svc.go\n│ │ └── error.go\n│ ├── pkg\n│ │ └── utils\n│ │ └── error.go\n│ └── router // Define routers "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("in")]),t._v(" this directory.\n│ ├── auth_router.go\n│ └── pub_router.go\n└── tests\n └── api\n └── demo.http\n")])])]),n("h2",{attrs:{id:"router"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#router"}},[t._v("#")]),t._v(" Router")]),t._v(" "),n("p",[t._v("In the directory "),n("code",[t._v("internal/router")]),t._v(", two "),n("code",[t._v("gin.IRouter")]),t._v("s are implemented:")]),t._v(" "),n("ul",[n("li",[t._v("pubRouter: Public router, endpoints mounted under this router can be accessed without authorization.")]),t._v(" "),n("li",[t._v("authRouter: Authenticated router, endpoints mounted under this router require authorization.")])]),t._v(" "),n("p",[t._v("Let's analyze the code in "),n("code",[t._v("internal/router/pub_router.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" router\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/middleware"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" IdRouterPub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"router-pub"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPubRouter")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("pubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" IdRouterPub\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" pubRouter "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter\n\troot gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v("\n\tpub "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("middleware"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PubMiddleware "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("pubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("root"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/api"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" r"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pub"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Next"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("ol",[n("li",[t._v("For routers, they need to implement the methods defined in "),n("code",[t._v("gone.IRouter")]),t._v(".")]),t._v(" "),n("li",[t._v("The struct "),n("code",[t._v("pubRouter")]),t._v(" embeds an "),n("code",[t._v("gone.IRouter")]),t._v(", which means it directly implements the "),n("code",[t._v("gone.IRouter")]),t._v(" interface. It is only assigned a value in "),n("code",[t._v("AfterRevive()")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v('r.IRouter = r.root.Group("/api", r.pub.Next)')]),t._v(" means the current router is a subrouter under the root router at "),n("code",[t._v("/api")]),t._v(", with an additional middleware "),n("code",[t._v("r.pub.Next")]),t._v(".")]),t._v(" "),n("li",[n("code",[t._v("root gone.IRouter")]),t._v(" is a framework-level Goner provided by the "),n("code",[t._v("github.com/gone-io/gone/goner/gin")]),t._v(" package, with a named injection "),n("code",[t._v("gone-gin-router")]),t._v(".")])]),t._v(" "),n("h2",{attrs:{id:"controller"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#controller"}},[t._v("#")]),t._v(" Controller")]),t._v(" "),n("p",[t._v("Below is the definition of the Controller interface:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Controller interface, implemented by business code to mount and handle routes")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// For usage, refer to [Sample Code](https://gitlab.openviewtech.com/gone/gone-example/-/tree/master/gone-app)")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Controller "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Mount Route mounting interface, this function will be called before service startup, and the implementation of this function should usually return `nil`")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" MountError\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("To write HTTP interfaces, we need to implement the "),n("code",[t._v("Controller")]),t._v(" interface and mount the interface routes in the "),n("code",[t._v("Mount")]),t._v(" method, as shown in the code in "),n("code",[t._v("internal/controller/demo_ctr.go")]),t._v(":")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" controller\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/service"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/pkg/utils"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoController")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Dependency injection of service")]),t._v("\n\n\tauthRouter gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-auth"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Router injection")]),t._v("\n\tpubRouter gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-pub"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Router injection")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tauthRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo2"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/error"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/echo"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("echo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("showDemo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tEcho "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"echo"`')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" req Req\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" ctx"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bind")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("req"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" utils"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ParameterParseError"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("req"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Echo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"service"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#service"}},[t._v("#")]),t._v(" Service")]),t._v(" "),n("p",[t._v("As per the standard, we require service interfaces to be defined in the "),n("code",[t._v("internal/interface/service")]),t._v(" directory. File names should begin with "),n("code",[t._v("i_")]),t._v(", and interface types should start with "),n("code",[t._v("I")]),t._v(", for example:")]),t._v(" "),n("p",[t._v("File: i_demo.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" service\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IDemo "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("DemoEntity"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("The logic implementation of services is placed in the "),n("code",[t._v("internal/module")]),t._v(" directory, organized into modules.")]),t._v(" "),n("h2",{attrs:{id:"use-database"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#use-database"}},[t._v("#")]),t._v(" "),n("a",{attrs:{href:"https://goner.fun/guide/xorm.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Use Database"),n("OutboundLink")],1)])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/44.ebd18468.js b/assets/js/43.c4dd240e.js similarity index 93% rename from assets/js/44.ebd18468.js rename to assets/js/43.c4dd240e.js index 0b13508..db54ed7 100644 --- a/assets/js/44.ebd18468.js +++ b/assets/js/43.c4dd240e.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[44],{328:function(t,e,n){"use strict";n.r(e);var o=n(10),r=Object(o.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"reference-documentation"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#reference-documentation"}},[t._v("#")]),t._v(" Reference Documentation")]),t._v(" "),e("h2",{attrs:{id:"gone-tool"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone-tool"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/references/gone-tool.html"}},[t._v("Gone Tool")])],1),t._v(" "),e("h2",{attrs:{id:"http-injection-instructions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-injection-instructions"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/references/http-inject.html"}},[t._v("HTTP Injection Instructions")])],1)])}),[],!1,null,null,null);e.default=r.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[43],{329:function(t,e,n){"use strict";n.r(e);var o=n(10),r=Object(o.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"reference-documentation"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#reference-documentation"}},[t._v("#")]),t._v(" Reference Documentation")]),t._v(" "),e("h2",{attrs:{id:"gone-tool"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone-tool"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/references/gone-tool.html"}},[t._v("Gone Tool")])],1),t._v(" "),e("h2",{attrs:{id:"http-injection-instructions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-injection-instructions"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/references/http-inject.html"}},[t._v("HTTP Injection Instructions")])],1)])}),[],!1,null,null,null);e.default=r.exports}}]); \ No newline at end of file diff --git a/assets/js/45.bf50206e.js b/assets/js/44.bfbe5fcc.js similarity index 98% rename from assets/js/45.bf50206e.js rename to assets/js/44.bfbe5fcc.js index 4580668..a982eaf 100644 --- a/assets/js/45.bf50206e.js +++ b/assets/js/44.bfbe5fcc.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[45],{330:function(a,t,s){"use strict";s.r(t);var e=s(10),n=Object(e.a)({},(function(){var a=this,t=a._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":a.$parent.slotKey}},[t("h1",{attrs:{id:"gone-tool"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#gone-tool"}},[a._v("#")]),a._v(" Gone Tool")]),a._v(" "),t("h2",{attrs:{id:"installation"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#installation"}},[a._v("#")]),a._v(" Installation")]),a._v(" "),t("p",[a._v("Run the following command:")]),a._v(" "),t("div",{staticClass:"language-bash extra-class"},[t("pre",{pre:!0,attrs:{class:"language-bash"}},[t("code",[a._v("go "),t("span",{pre:!0,attrs:{class:"token function"}},[a._v("install")]),a._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),t("h2",{attrs:{id:"usage"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#usage"}},[a._v("#")]),a._v(" Usage")]),a._v(" "),t("h3",{attrs:{id:"testing-installation"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#testing-installation"}},[a._v("#")]),a._v(" Testing Installation")]),a._v(" "),t("p",[a._v("After successful installation, run "),t("code",[a._v("gone -h")]),a._v(" to verify the installation. You should see the following output:")]),a._v(" "),t("div",{staticClass:"language-bash extra-class"},[t("pre",{pre:!0,attrs:{class:"language-bash"}},[t("code",[a._v("➜ demo gone "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-h")]),a._v("\nNAME:\n gone - A new cli application\n\nUSAGE:\n gone "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("global options"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("command")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("command options"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("arguments"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("..")]),a._v("."),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v("\n\nDESCRIPTION:\n generate gone code or generate gone app\n\nCOMMANDS:\n priest "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-s")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${scanPackageDir}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-p")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${pkgName}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-f")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${funcName}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-o")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${outputFilePath}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-w"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v("\n mock "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-f")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${fromGoFile}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-o")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${outGoFile}")]),a._v("\n create "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-t "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${template}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-m "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${modName}")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${appName}")]),a._v("\n help, h Shows a list of commands or "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("help")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token keyword"}},[a._v("for")]),a._v(" one "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("command")]),a._v("\n\nGLOBAL OPTIONS:\n --help, "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-h")]),a._v(" show "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("help")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("(")]),a._v("default: "),t("span",{pre:!0,attrs:{class:"token boolean"}},[a._v("false")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v(")")]),a._v("\n")])])]),t("h3",{attrs:{id:"generate-project"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#generate-project"}},[a._v("#")]),a._v(" Generate Project")]),a._v(" "),t("h3",{attrs:{id:"generate-priest-function"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#generate-priest-function"}},[a._v("#")]),a._v(" Generate Priest Function")])])}),[],!1,null,null,null);t.default=n.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[44],{330:function(a,t,s){"use strict";s.r(t);var e=s(10),n=Object(e.a)({},(function(){var a=this,t=a._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":a.$parent.slotKey}},[t("h1",{attrs:{id:"gone-tool"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#gone-tool"}},[a._v("#")]),a._v(" Gone Tool")]),a._v(" "),t("h2",{attrs:{id:"installation"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#installation"}},[a._v("#")]),a._v(" Installation")]),a._v(" "),t("p",[a._v("Run the following command:")]),a._v(" "),t("div",{staticClass:"language-bash extra-class"},[t("pre",{pre:!0,attrs:{class:"language-bash"}},[t("code",[a._v("go "),t("span",{pre:!0,attrs:{class:"token function"}},[a._v("install")]),a._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),t("h2",{attrs:{id:"usage"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#usage"}},[a._v("#")]),a._v(" Usage")]),a._v(" "),t("h3",{attrs:{id:"testing-installation"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#testing-installation"}},[a._v("#")]),a._v(" Testing Installation")]),a._v(" "),t("p",[a._v("After successful installation, run "),t("code",[a._v("gone -h")]),a._v(" to verify the installation. You should see the following output:")]),a._v(" "),t("div",{staticClass:"language-bash extra-class"},[t("pre",{pre:!0,attrs:{class:"language-bash"}},[t("code",[a._v("➜ demo gone "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-h")]),a._v("\nNAME:\n gone - A new cli application\n\nUSAGE:\n gone "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("global options"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("command")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("command options"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("arguments"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("..")]),a._v("."),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v("\n\nDESCRIPTION:\n generate gone code or generate gone app\n\nCOMMANDS:\n priest "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-s")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${scanPackageDir}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-p")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${pkgName}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-f")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${funcName}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-o")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${outputFilePath}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-w"),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v("\n mock "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-f")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${fromGoFile}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-o")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${outGoFile}")]),a._v("\n create "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-t "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${template}")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("[")]),a._v("-m "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${modName}")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("]")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token variable"}},[a._v("${appName}")]),a._v("\n help, h Shows a list of commands or "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("help")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token keyword"}},[a._v("for")]),a._v(" one "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("command")]),a._v("\n\nGLOBAL OPTIONS:\n --help, "),t("span",{pre:!0,attrs:{class:"token parameter variable"}},[a._v("-h")]),a._v(" show "),t("span",{pre:!0,attrs:{class:"token builtin class-name"}},[a._v("help")]),a._v(" "),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v("(")]),a._v("default: "),t("span",{pre:!0,attrs:{class:"token boolean"}},[a._v("false")]),t("span",{pre:!0,attrs:{class:"token punctuation"}},[a._v(")")]),a._v("\n")])])]),t("h3",{attrs:{id:"generate-project"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#generate-project"}},[a._v("#")]),a._v(" Generate Project")]),a._v(" "),t("h3",{attrs:{id:"generate-priest-function"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#generate-priest-function"}},[a._v("#")]),a._v(" Generate Priest Function")])])}),[],!1,null,null,null);t.default=n.exports}}]); \ No newline at end of file diff --git a/assets/js/46.5263e285.js b/assets/js/45.5d30e29b.js similarity index 99% rename from assets/js/46.5263e285.js rename to assets/js/45.5d30e29b.js index 3847154..9729a70 100644 --- a/assets/js/46.5263e285.js +++ b/assets/js/45.5d30e29b.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[46],{331:function(t,e,n){"use strict";n.r(e);var s=n(10),a=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"http-injection-instructions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-injection-instructions"}},[t._v("#")]),t._v(" HTTP Injection Instructions")]),t._v(" "),e("h2",{attrs:{id:"format-of-http-dependency-injection-tags"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#format-of-http-dependency-injection-tags"}},[t._v("#")]),t._v(" Format of HTTP Dependency Injection Tags")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('${attributeName} ${attributeType} gone:"http,${kind}=${key}"\n')])])]),e("p",[t._v("Example:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("router"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The injected value of in.selects will be `[]int{1,2,3}`")]),t._v("\n fmt"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("p",[t._v("In the above example:")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("selects")]),t._v(" is the attribute name.")]),t._v(" "),e("li",[e("code",[t._v("[]int")]),t._v(" is the attribute type.")]),t._v(" "),e("li",[e("code",[t._v("query")]),t._v(" is the injection type.")]),t._v(" "),e("li",[e("code",[t._v("select")]),t._v(" is the injection key.")])]),t._v(" "),e("h2",{attrs:{id:"supported-injection-types-and-response-tags"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supported-injection-types-and-response-tags"}},[t._v("#")]),t._v(" Supported Injection Types and Response Tags")]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Name")]),t._v(" "),e("th",[t._v("Attribute Type "),e("code",[t._v("${attributeType}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"center"}},[t._v("Injection Type "),e("code",[t._v("${kind}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"center"}},[t._v("Injection Key "),e("code",[t._v("${key}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"left"}},[t._v("Description")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("strong",[t._v("Context Injection")])]),t._v(" "),e("td",[e("code",[t._v("gone.Context")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the gin request context object. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Context Injection")])]),t._v(" "),e("td",[e("code",[t._v("*gone.Context")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the gin request context pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Request Injection")])]),t._v(" "),e("td",[e("code",[t._v("http.Request")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the http.Request object. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Request Injection")])]),t._v(" "),e("td",[e("code",[t._v("*http.Request")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the http.Request pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Injection")])]),t._v(" "),e("td",[e("code",[t._v("url.URL")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the url.URL. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Injection")])]),t._v(" "),e("td",[e("code",[t._v("*url.URL")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the url.URL pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Header Injection")])]),t._v(" "),e("td",[e("code",[t._v("http.Header")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the http.Header (request headers). "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Response Injection")])]),t._v(" "),e("td",[e("code",[t._v("gone.ResponseWriter")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Injects the gin.ResponseWriter (used to directly write response data). "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Body Injection")])]),t._v(" "),e("td",[t._v("Struct, Struct Pointer")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[e("code",[t._v("body")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[e("strong",[t._v("Body Injection")]),t._v("; parses the request body and injects it into the attribute. The injection type is "),e("code",[t._v("body")]),t._v(", and "),e("code",[t._v("${key}")]),t._v(" is not needed. The framework automatically determines the format (e.g., JSON, XML) based on "),e("code",[t._v("Content-Type")]),t._v(". Only one "),e("strong",[t._v("body injection")]),t._v(" is allowed per request handler.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Single Value Header Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("header")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the request header with the key "),e("code",[t._v("${key}")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Parameter Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("param")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the URL parameter with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Param(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Query Parameter Injection")])]),t._v(" "),e("td",[t._v("number | string | []number | []string | Struct | Struct Pointer")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("query")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the query parameter with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Query(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc., and "),e("strong",[t._v("supports arrays"),e("sup",[t._v("[1]")])]),t._v(". If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Cookie Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("cookie")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the cookie with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Context.Cookie(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])])])]),t._v(" "),e("h2",{attrs:{id:"notes"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#notes"}},[t._v("#")]),t._v(" Notes")]),t._v(" "),e("p",[t._v("[1]. Query parameter injection supports arrays. For example:\nIf the query is "),e("code",[t._v("?select=1&select=2&select=3")]),t._v(", the value of "),e("code",[t._v("in.selects")]),t._v(" in the code below will be "),e("code",[t._v("[]int{1,2,3}")]),t._v(".")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("router"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The injected value of in.selects will be `[]int{1,2,3}`")]),t._v("\n fmt"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[45],{332:function(t,e,n){"use strict";n.r(e);var s=n(10),a=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"http-injection-instructions"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-injection-instructions"}},[t._v("#")]),t._v(" HTTP Injection Instructions")]),t._v(" "),e("h2",{attrs:{id:"format-of-http-dependency-injection-tags"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#format-of-http-dependency-injection-tags"}},[t._v("#")]),t._v(" Format of HTTP Dependency Injection Tags")]),t._v(" "),e("div",{staticClass:"language- extra-class"},[e("pre",{pre:!0,attrs:{class:"language-text"}},[e("code",[t._v('${attributeName} ${attributeType} gone:"http,${kind}=${key}"\n')])])]),e("p",[t._v("Example:")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("router"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The injected value of in.selects will be `[]int{1,2,3}`")]),t._v("\n fmt"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),e("p",[t._v("In the above example:")]),t._v(" "),e("ul",[e("li",[e("code",[t._v("selects")]),t._v(" is the attribute name.")]),t._v(" "),e("li",[e("code",[t._v("[]int")]),t._v(" is the attribute type.")]),t._v(" "),e("li",[e("code",[t._v("query")]),t._v(" is the injection type.")]),t._v(" "),e("li",[e("code",[t._v("select")]),t._v(" is the injection key.")])]),t._v(" "),e("h2",{attrs:{id:"supported-injection-types-and-response-tags"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#supported-injection-types-and-response-tags"}},[t._v("#")]),t._v(" Supported Injection Types and Response Tags")]),t._v(" "),e("table",[e("thead",[e("tr",[e("th",[t._v("Name")]),t._v(" "),e("th",[t._v("Attribute Type "),e("code",[t._v("${attributeType}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"center"}},[t._v("Injection Type "),e("code",[t._v("${kind}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"center"}},[t._v("Injection Key "),e("code",[t._v("${key}")])]),t._v(" "),e("th",{staticStyle:{"text-align":"left"}},[t._v("Description")])])]),t._v(" "),e("tbody",[e("tr",[e("td",[e("strong",[t._v("Context Injection")])]),t._v(" "),e("td",[e("code",[t._v("gone.Context")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the gin request context object. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Context Injection")])]),t._v(" "),e("td",[e("code",[t._v("*gone.Context")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the gin request context pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Request Injection")])]),t._v(" "),e("td",[e("code",[t._v("http.Request")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the http.Request object. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Request Injection")])]),t._v(" "),e("td",[e("code",[t._v("*http.Request")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the http.Request pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Injection")])]),t._v(" "),e("td",[e("code",[t._v("url.URL")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Not recommended) Injects the url.URL. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Injection")])]),t._v(" "),e("td",[e("code",[t._v("*url.URL")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the url.URL pointer. "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Header Injection")])]),t._v(" "),e("td",[e("code",[t._v("http.Header")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("(Recommended) Injects the http.Header (request headers). "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Response Injection")])]),t._v(" "),e("td",[e("code",[t._v("gone.ResponseWriter")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Injects the gin.ResponseWriter (used to directly write response data). "),e("code",[t._v("${kind}")]),t._v(" and "),e("code",[t._v("${key}")]),t._v(" are not needed.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Body Injection")])]),t._v(" "),e("td",[t._v("Struct, Struct Pointer")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[e("code",[t._v("body")])]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[e("strong",[t._v("Body Injection")]),t._v("; parses the request body and injects it into the attribute. The injection type is "),e("code",[t._v("body")]),t._v(", and "),e("code",[t._v("${key}")]),t._v(" is not needed. The framework automatically determines the format (e.g., JSON, XML) based on "),e("code",[t._v("Content-Type")]),t._v(". Only one "),e("strong",[t._v("body injection")]),t._v(" is allowed per request handler.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Single Value Header Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("header")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the request header with the key "),e("code",[t._v("${key}")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("URL Parameter Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("param")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the URL parameter with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Param(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Query Parameter Injection")])]),t._v(" "),e("td",[t._v("number | string | []number | []string | Struct | Struct Pointer")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("query")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the query parameter with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Query(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc., and "),e("strong",[t._v("supports arrays"),e("sup",[t._v("[1]")])]),t._v(". If parsing fails, an argument error is returned.")])]),t._v(" "),e("tr",[e("td",[e("strong",[t._v("Cookie Injection")])]),t._v(" "),e("td",[t._v("number | string")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("cookie")]),t._v(" "),e("td",{staticStyle:{"text-align":"center"}},[t._v("Defaults to field name")]),t._v(" "),e("td",{staticStyle:{"text-align":"left"}},[t._v("Retrieves the cookie with the key "),e("code",[t._v("${key}")]),t._v(" using "),e("code",[t._v("ctx.Context.Cookie(key)")]),t._v(". Attribute types support "),e("code",[t._v("string")]),t._v(", "),e("code",[t._v("int")]),t._v(", "),e("code",[t._v("uint")]),t._v(", "),e("code",[t._v("float64")]),t._v(", etc. If parsing fails, an argument error is returned.")])])])]),t._v(" "),e("h2",{attrs:{id:"notes"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#notes"}},[t._v("#")]),t._v(" Notes")]),t._v(" "),e("p",[t._v("[1]. Query parameter injection supports arrays. For example:\nIf the query is "),e("code",[t._v("?select=1&select=2&select=3")]),t._v(", the value of "),e("code",[t._v("in.selects")]),t._v(" in the code below will be "),e("code",[t._v("[]int{1,2,3}")]),t._v(".")]),t._v(" "),e("div",{staticClass:"language-go extra-class"},[e("pre",{pre:!0,attrs:{class:"language-go"}},[e("code",[t._v("router"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),e("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),e("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),e("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// The injected value of in.selects will be `[]int{1,2,3}`")]),t._v("\n fmt"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),e("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),e("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),e("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/47.8a385ced.js b/assets/js/46.9964cc27.js similarity index 99% rename from assets/js/47.8a385ced.js rename to assets/js/46.9964cc27.js index 6cef564..f01fcca 100644 --- a/assets/js/47.8a385ced.js +++ b/assets/js/46.9964cc27.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[47],{332:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"介绍"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#介绍"}},[t._v("#")]),t._v(" 介绍")]),t._v(" "),s("h2",{attrs:{id:"gone是什么"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone是什么"}},[t._v("#")]),t._v(" Gone是什么")]),t._v(" "),s("p",[t._v("首先,"),s("strong",[t._v("Gone")]),t._v("是一个轻量的,基于"),s("strong",[t._v("Golang")]),t._v("的,"),s("strong",[t._v("依赖注入框架")]),t._v(",灵感来源于Java中的Spring Framework;其次,"),s("strong",[t._v("Gone")]),t._v("框架中包含了一系列内置组件,通过这些组件提供一整套Web开发方案,提供服务配置、日志追踪、服务调用、数据库访问、消息中间件等微服务常用能力。")]),t._v(" "),s("p",[t._v("下面使用"),s("strong",[t._v("Gone")]),t._v("来编写一个Web服务吧!")]),t._v(" "),s("h2",{attrs:{id:"web服务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web服务"}},[t._v("#")]),t._v(" Web服务")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 实现一个Goner,什么是Goner? => https://goner.fun/zh/guide/core-concept.html#goner-%E9%80%9D%E8%80%85")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//goner 标记,匿名嵌入后,一个结构体就实现了Goner")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 实现 Mount 方法,挂载路由;框架会自动执行该方法")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定义请求结构体")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tMsg "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"msg"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册 `POST /hello` 的 处理函数")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tto "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入http请求Query参数To")]),t._v("\n\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入http请求Body")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to %s msg is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("to"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Msg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//启动服务")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用框架内置组件,加载gin框架")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//将 一个controller类型的Goner埋葬到墓园")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬是什么意思? => https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//墓园是什么意思? => https://goner.fun/zh/guide/core-concept.html#cemetery-%E5%A2%93%E5%9B%AD")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("运行上面代码:"),s("code",[t._v("go run main.go")]),t._v(",程序将监听"),s("code",[t._v("8080")]),t._v("端口,使用curl测试:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("curl")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-X")]),t._v(" POST "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'http://localhost:8080/hello'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-H")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Content-Type: application/json'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n\t--data-raw "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('\'{"msg": "你好呀?"}\'')]),t._v("\n")])])]),s("p",[t._v("结果如下:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"code"')]),t._v(":0,"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to msg is: 你好呀?"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"概念"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#概念"}},[t._v("#")]),t._v(" 概念")]),t._v(" "),s("blockquote",[s("p",[t._v("我们编写的代码终究只是死物,除非他们被运行起来。")])]),t._v(" "),s("p",[t._v("在Gone中,组件被抽象为"),s("strong",[t._v("Goner(逝者)")]),t._v(","),s("strong",[t._v("Goner")]),t._v("属性可以注入其他的"),s("strong",[t._v("Goner")]),t._v("。Gone启动前,需要将所有 "),s("strong",[t._v("Goners")]),t._v(" "),s("strong",[t._v("埋葬(Bury)"),s("strong",[t._v("到")]),t._v("墓园(cemetery)")]),t._v(";Gone启动后,会将所有 "),s("strong",[t._v("Goners")]),t._v(" "),s("strong",[t._v("复活")]),t._v(",建立一个 "),s("strong",[t._v("天国(Heaven)")]),t._v(",“天国的所有人都不再残缺,他们想要的必定得到满足”。")]),t._v(" "),s("p",[t._v("了解更多,请阅读 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[46],{335:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"介绍"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#介绍"}},[t._v("#")]),t._v(" 介绍")]),t._v(" "),s("h2",{attrs:{id:"gone是什么"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone是什么"}},[t._v("#")]),t._v(" Gone是什么")]),t._v(" "),s("p",[t._v("首先,"),s("strong",[t._v("Gone")]),t._v("是一个轻量的,基于"),s("strong",[t._v("Golang")]),t._v("的,"),s("strong",[t._v("依赖注入框架")]),t._v(",灵感来源于Java中的Spring Framework;其次,"),s("strong",[t._v("Gone")]),t._v("框架中包含了一系列内置组件,通过这些组件提供一整套Web开发方案,提供服务配置、日志追踪、服务调用、数据库访问、消息中间件等微服务常用能力。")]),t._v(" "),s("p",[t._v("下面使用"),s("strong",[t._v("Gone")]),t._v("来编写一个Web服务吧!")]),t._v(" "),s("h2",{attrs:{id:"web服务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web服务"}},[t._v("#")]),t._v(" Web服务")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 实现一个Goner,什么是Goner? => https://goner.fun/zh/guide/core-concept.html#goner-%E9%80%9D%E8%80%85")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//goner 标记,匿名嵌入后,一个结构体就实现了Goner")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 实现 Mount 方法,挂载路由;框架会自动执行该方法")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定义请求结构体")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tMsg "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"msg"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册 `POST /hello` 的 处理函数")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tto "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入http请求Query参数To")]),t._v("\n\t\treq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入http请求Body")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to %s msg is: %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("to"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Msg"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//启动服务")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用框架内置组件,加载gin框架")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//将 一个controller类型的Goner埋葬到墓园")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬是什么意思? => https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//墓园是什么意思? => https://goner.fun/zh/guide/core-concept.html#cemetery-%E5%A2%93%E5%9B%AD")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("运行上面代码:"),s("code",[t._v("go run main.go")]),t._v(",程序将监听"),s("code",[t._v("8080")]),t._v("端口,使用curl测试:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("curl")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-X")]),t._v(" POST "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'http://localhost:8080/hello'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-H")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'Content-Type: application/json'")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("\\")]),t._v("\n\t--data-raw "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('\'{"msg": "你好呀?"}\'')]),t._v("\n")])])]),s("p",[t._v("结果如下:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"code"')]),t._v(":0,"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"to msg is: 你好呀?"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"概念"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#概念"}},[t._v("#")]),t._v(" 概念")]),t._v(" "),s("blockquote",[s("p",[t._v("我们编写的代码终究只是死物,除非他们被运行起来。")])]),t._v(" "),s("p",[t._v("在Gone中,组件被抽象为"),s("strong",[t._v("Goner(逝者)")]),t._v(","),s("strong",[t._v("Goner")]),t._v("属性可以注入其他的"),s("strong",[t._v("Goner")]),t._v("。Gone启动前,需要将所有 "),s("strong",[t._v("Goners")]),t._v(" "),s("strong",[t._v("埋葬(Bury)"),s("strong",[t._v("到")]),t._v("墓园(cemetery)")]),t._v(";Gone启动后,会将所有 "),s("strong",[t._v("Goners")]),t._v(" "),s("strong",[t._v("复活")]),t._v(",建立一个 "),s("strong",[t._v("天国(Heaven)")]),t._v(",“天国的所有人都不再残缺,他们想要的必定得到满足”。")]),t._v(" "),s("p",[t._v("了解更多,请阅读 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/48.fbe07ee4.js b/assets/js/47.dabffb29.js similarity index 91% rename from assets/js/48.fbe07ee4.js rename to assets/js/47.dabffb29.js index 2125799..13cb354 100644 --- a/assets/js/48.fbe07ee4.js +++ b/assets/js/47.dabffb29.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[48],{341:function(t,s,r){"use strict";r.r(s);var e=r(10),a=Object(e.a)({},(function(){var t=this._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":this.$parent.slotKey}},[t("h1",{attrs:{id:"博客"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#博客"}},[this._v("#")]),this._v(" 博客")]),this._v(" "),t("h2",{attrs:{id:"v1-x版本-正式发布"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#v1-x版本-正式发布"}},[this._v("#")]),this._v(" "),t("a",{attrs:{href:"https://goner.fun/zh/blogs/1_v1.x_release.html",target:"_blank",rel:"noopener noreferrer"}},[this._v("v1.x版本 正式发布"),t("OutboundLink")],1)])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[47],{334:function(t,s,r){"use strict";r.r(s);var e=r(10),a=Object(e.a)({},(function(){var t=this._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":this.$parent.slotKey}},[t("h1",{attrs:{id:"博客"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#博客"}},[this._v("#")]),this._v(" 博客")]),this._v(" "),t("h2",{attrs:{id:"v1-x版本-正式发布"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#v1-x版本-正式发布"}},[this._v("#")]),this._v(" "),t("a",{attrs:{href:"https://goner.fun/zh/blogs/1_v1.x_release.html",target:"_blank",rel:"noopener noreferrer"}},[this._v("v1.x版本 正式发布"),t("OutboundLink")],1)])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/49.327cc8c3.js b/assets/js/48.89c14ae9.js similarity index 98% rename from assets/js/49.327cc8c3.js rename to assets/js/48.89c14ae9.js index 00fce21..617a7ae 100644 --- a/assets/js/49.327cc8c3.js +++ b/assets/js/48.89c14ae9.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[49],{338:function(e,t,r){"use strict";r.r(t);var o=r(10),v=Object(o.a)({},(function(){var e=this,t=e._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#goners"}},[e._v("#")]),e._v(" Goners")]),e._v(" "),t("h2",{attrs:{id:"核心级goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#核心级goners"}},[e._v("#")]),e._v(" 核心级Goners")]),e._v(" "),t("blockquote",[t("p",[e._v("核心组件也是Goner,可以被其他Goner注入,这部分Goners,是Gone运行的基础,是Gone运行不可或缺的部分。")])]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("接口名")]),e._v(" "),t("th",[e._v("实现")]),e._v(" "),t("th",[e._v("GoneId")]),e._v(" "),t("th",[e._v("功能介绍")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Heaven",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/heaven.go#L65",target:"_blank",rel:"noopener noreferrer"}},[e._v("heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-heaven")]),e._v(" "),t("td",[e._v("负责将Goner从cemetery中复活,执行安装的Hook函数,管理框架的启停流程和状态。")])]),e._v(" "),t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Cemetery",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/cemetery.go#L17",target:"_blank",rel:"noopener noreferrer"}},[e._v("cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-cemetery")]),e._v(" "),t("td",[e._v("管理Goners,提供将Goner埋葬的Bury方法;依赖注入的逻辑主要在该结构体中实现。")])])])]),e._v(" "),t("h2",{attrs:{id:"框架内置goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#框架内置goners"}},[e._v("#")]),e._v(" 框架内置Goners")]),e._v(" "),t("p",[e._v("框架内置Goners,是为丰富Gone的功能而开发的,比如支持Web开发、支持数据库连接、支持Redis等…"),t("br"),e._v("\n代码实现在 "),t("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.4/goner",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner目录"),t("OutboundLink")],1),e._v(",该目录下的每个子目录分别实现了一个gone的特性,每个特性包含一个或多个Goner的定义和实现。")]),e._v(" "),t("p",[e._v("为了方便使用,我们在 "),t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/v0.1.4/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner/priest.go"),t("OutboundLink")],1),e._v(" 中定义了"),t("code",[e._v("Priest")]),e._v("函数,可以按功能直接批量埋葬相关Goners。")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("目录/组件")]),e._v(" "),t("th",[e._v("实现功能")]),e._v(" "),t("th",[e._v("文档")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[e._v("config")]),e._v(" "),t("td",[e._v("读取config目录下的配置文件,允许配置项注入到Goner")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/config.html"}},[e._v("通过内置Goners支持配置文件")])],1)]),e._v(" "),t("tr",[t("td",[e._v("logrus")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/sirupsen/logrus")]),e._v(",提供日志打印相关的方法,支持将日志按格式打印")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/logrus.html"}},[e._v("日志打印")])],1)]),e._v(" "),t("tr",[t("td",[e._v("tracer")]),e._v(" "),t("td",[e._v("日志追踪,提供traceId;在处理同一请求时,日志打印可以拥有相同的traceId")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/tracer.html"}},[e._v("使用traceId追踪日志")])],1)]),e._v(" "),t("tr",[t("td",[e._v("gin")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/gin-gonic/gin")]),e._v(",使gone支持web开发")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("xorm")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("xorm.io/xorm")]),e._v(",用于操作数据")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/xorm.html"}},[e._v("通过内置Goners支持数据库连接")])],1)]),e._v(" "),t("tr",[t("td",[e._v("redis")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/gomodule/redigo/redis")]),e._v(",用于操作redis,提供redis缓存和redis分布式锁的功能")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/redis.html"}},[e._v("利用redis提供分布式锁和分布式缓存")])],1)]),e._v(" "),t("tr",[t("td",[e._v("schedule")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/robfig/cron/v3")]),e._v(",提供定时任务的能力")]),e._v(" "),t("td",[t("a",{attrs:{href:"https://goner.fun/zh/guide/schedule.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("用cron表达式配置定时任务"),t("OutboundLink")],1)])]),e._v(" "),t("tr",[t("td",[e._v("urllib")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/imroc/req/v3")]),e._v(",提供http调用能力")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("grpc")]),e._v(" "),t("td",[e._v("提供开发grpc服务端和客户端的能力")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("cmux")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/soheilhy/cmux")]),e._v(",是统一端口可以提供混合服务的能力")]),e._v(" "),t("td",[e._v("-")])])])]),e._v(" "),t("h2",{attrs:{id:"生态级-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#生态级-goners"}},[e._v("#")]),e._v(" 生态级 Goners")]),e._v(" "),t("ul",[t("li",[t("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[e._v("emitter"),t("OutboundLink")],1),e._v(",封装事件处理,可以用于 DDD 的 事件风暴")])])])}),[],!1,null,null,null);t.default=v.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[48],{337:function(e,t,r){"use strict";r.r(t);var o=r(10),v=Object(o.a)({},(function(){var e=this,t=e._self._c;return t("ContentSlotsDistributor",{attrs:{"slot-key":e.$parent.slotKey}},[t("h1",{attrs:{id:"goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#goners"}},[e._v("#")]),e._v(" Goners")]),e._v(" "),t("h2",{attrs:{id:"核心级goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#核心级goners"}},[e._v("#")]),e._v(" 核心级Goners")]),e._v(" "),t("blockquote",[t("p",[e._v("核心组件也是Goner,可以被其他Goner注入,这部分Goners,是Gone运行的基础,是Gone运行不可或缺的部分。")])]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("接口名")]),e._v(" "),t("th",[e._v("实现")]),e._v(" "),t("th",[e._v("GoneId")]),e._v(" "),t("th",[e._v("功能介绍")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Heaven",target:"_blank",rel:"noopener noreferrer"}},[e._v("Heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/heaven.go#L65",target:"_blank",rel:"noopener noreferrer"}},[e._v("heaven"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-heaven")]),e._v(" "),t("td",[e._v("负责将Goner从cemetery中复活,执行安装的Hook函数,管理框架的启停流程和状态。")])]),e._v(" "),t("tr",[t("td",[t("a",{attrs:{href:"https://pkg.go.dev/github.com/gone-io/gone#Cemetery",target:"_blank",rel:"noopener noreferrer"}},[e._v("Cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/12ea8e3577fbed493766f522ab002136edf3805d/cemetery.go#L17",target:"_blank",rel:"noopener noreferrer"}},[e._v("cemetery"),t("OutboundLink")],1)]),e._v(" "),t("td",[e._v("gone-cemetery")]),e._v(" "),t("td",[e._v("管理Goners,提供将Goner埋葬的Bury方法;依赖注入的逻辑主要在该结构体中实现。")])])])]),e._v(" "),t("h2",{attrs:{id:"框架内置goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#框架内置goners"}},[e._v("#")]),e._v(" 框架内置Goners")]),e._v(" "),t("p",[e._v("框架内置Goners,是为丰富Gone的功能而开发的,比如支持Web开发、支持数据库连接、支持Redis等…"),t("br"),e._v("\n代码实现在 "),t("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.4/goner",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner目录"),t("OutboundLink")],1),e._v(",该目录下的每个子目录分别实现了一个gone的特性,每个特性包含一个或多个Goner的定义和实现。")]),e._v(" "),t("p",[e._v("为了方便使用,我们在 "),t("a",{attrs:{href:"https://github.com/gone-io/gone/blob/v0.1.4/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[e._v("goner/priest.go"),t("OutboundLink")],1),e._v(" 中定义了"),t("code",[e._v("Priest")]),e._v("函数,可以按功能直接批量埋葬相关Goners。")]),e._v(" "),t("table",[t("thead",[t("tr",[t("th",[e._v("目录/组件")]),e._v(" "),t("th",[e._v("实现功能")]),e._v(" "),t("th",[e._v("文档")])])]),e._v(" "),t("tbody",[t("tr",[t("td",[e._v("config")]),e._v(" "),t("td",[e._v("读取config目录下的配置文件,允许配置项注入到Goner")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/config.html"}},[e._v("通过内置Goners支持配置文件")])],1)]),e._v(" "),t("tr",[t("td",[e._v("logrus")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/sirupsen/logrus")]),e._v(",提供日志打印相关的方法,支持将日志按格式打印")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/logrus.html"}},[e._v("日志打印")])],1)]),e._v(" "),t("tr",[t("td",[e._v("tracer")]),e._v(" "),t("td",[e._v("日志追踪,提供traceId;在处理同一请求时,日志打印可以拥有相同的traceId")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/tracer.html"}},[e._v("使用traceId追踪日志")])],1)]),e._v(" "),t("tr",[t("td",[e._v("gin")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/gin-gonic/gin")]),e._v(",使gone支持web开发")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("xorm")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("xorm.io/xorm")]),e._v(",用于操作数据")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/xorm.html"}},[e._v("通过内置Goners支持数据库连接")])],1)]),e._v(" "),t("tr",[t("td",[e._v("redis")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/gomodule/redigo/redis")]),e._v(",用于操作redis,提供redis缓存和redis分布式锁的功能")]),e._v(" "),t("td",[t("RouterLink",{attrs:{to:"/zh/guide/redis.html"}},[e._v("利用redis提供分布式锁和分布式缓存")])],1)]),e._v(" "),t("tr",[t("td",[e._v("schedule")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/robfig/cron/v3")]),e._v(",提供定时任务的能力")]),e._v(" "),t("td",[t("a",{attrs:{href:"https://goner.fun/zh/guide/schedule.html",target:"_blank",rel:"noopener noreferrer"}},[e._v("用cron表达式配置定时任务"),t("OutboundLink")],1)])]),e._v(" "),t("tr",[t("td",[e._v("urllib")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/imroc/req/v3")]),e._v(",提供http调用能力")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("grpc")]),e._v(" "),t("td",[e._v("提供开发grpc服务端和客户端的能力")]),e._v(" "),t("td",[e._v("-")])]),e._v(" "),t("tr",[t("td",[e._v("cmux")]),e._v(" "),t("td",[e._v("封装"),t("code",[e._v("github.com/soheilhy/cmux")]),e._v(",是统一端口可以提供混合服务的能力")]),e._v(" "),t("td",[e._v("-")])])])]),e._v(" "),t("h2",{attrs:{id:"生态级-goners"}},[t("a",{staticClass:"header-anchor",attrs:{href:"#生态级-goners"}},[e._v("#")]),e._v(" 生态级 Goners")]),e._v(" "),t("ul",[t("li",[t("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[e._v("emitter"),t("OutboundLink")],1),e._v(",封装事件处理,可以用于 DDD 的 事件风暴")])])])}),[],!1,null,null,null);t.default=v.exports}}]); \ No newline at end of file diff --git a/assets/js/50.64512e4a.js b/assets/js/49.1db4f10e.js similarity index 98% rename from assets/js/50.64512e4a.js rename to assets/js/49.1db4f10e.js index 9a3363e..1e25f9b 100644 --- a/assets/js/50.64512e4a.js +++ b/assets/js/49.1db4f10e.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[50],{335:function(t,r,a){"use strict";a.r(r);var s=a(10),e=Object(s.a)({},(function(){var t=this,r=t._self._c;return r("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[r("h1",{attrs:{id:"开发指南"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#开发指南"}},[t._v("#")]),t._v(" 开发指南")]),t._v(" "),r("h2",{attrs:{id:"gone的核心概念"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#gone的核心概念"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/core-concept.html"}},[t._v("Gone的核心概念")])],1),t._v(" "),r("h2",{attrs:{id:"依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#依赖注入"}},[t._v("#")]),t._v(" 依赖注入")]),t._v(" "),r("h3",{attrs:{id:"goner和依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#goner和依赖注入"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./gone-and-inject"}},[t._v("goner和依赖注入")])]),t._v(" "),r("h3",{attrs:{id:"gone支持哪些方式注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#gone支持哪些方式注入"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/goner-inject.html"}},[t._v("Gone支持哪些方式注入?")])],1),t._v(" "),r("h3",{attrs:{id:"对比-gone-和-spring-的依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#对比-gone-和-spring-的依赖注入"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/gone-vs-spring.html"}},[t._v("对比 Gone 和 Spring 的依赖注入")])],1),t._v(" "),r("h2",{attrs:{id:"内置模块使用"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#内置模块使用"}},[t._v("#")]),t._v(" 内置模块使用")]),t._v(" "),r("h3",{attrs:{id:"如何优雅使用内置goners"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#如何优雅使用内置goners"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/inner-goner.html"}},[t._v("如何优雅使用内置Goners?")])],1),t._v(" "),r("h3",{attrs:{id:"通过内置goners支持配置文件"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持配置文件"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/config.html"}},[t._v("通过内置Goners支持配置文件")])],1),t._v(" "),r("h3",{attrs:{id:"日志输出"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#日志输出"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/logrus.html"}},[t._v("日志输出")])],1),t._v(" "),r("h3",{attrs:{id:"使用traceid追踪日志"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#使用traceid追踪日志"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/tracer.html"}},[t._v("使用traceId追踪日志")])],1),t._v(" "),r("h3",{attrs:{id:"通过内置goners支持数据库连接"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持数据库连接"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/xorm.html"}},[t._v("通过内置Goners支持数据库连接")])],1),t._v(" "),r("h3",{attrs:{id:"利用redis提供分布式锁和分布式缓存"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#利用redis提供分布式锁和分布式缓存"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./redis"}},[t._v("利用redis提供分布式锁和分布式缓存")])]),t._v(" "),r("h3",{attrs:{id:"用cron表达式配置定时任务"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#用cron表达式配置定时任务"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./schedule"}},[t._v("用cron表达式配置定时任务")])]),t._v(" "),r("h2",{attrs:{id:"效率提升"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#效率提升"}},[t._v("#")]),t._v(" 效率提升")]),t._v(" "),r("h3",{attrs:{id:"自动生成priest"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#自动生成priest"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/auto-gen-priest.html"}},[t._v("自动生成Priest")])],1),t._v(" "),r("h3",{attrs:{id:"hook函数"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#hook函数"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/hooks.html"}},[t._v("Hook函数")])],1),t._v(" "),r("h3",{attrs:{id:"错误处理"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#错误处理"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./error"}},[t._v("错误处理")])]),t._v(" "),r("h3",{attrs:{id:"单元测试"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#单元测试"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./unit-test"}},[t._v("单元测试")])])])}),[],!1,null,null,null);r.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[49],{350:function(t,r,a){"use strict";a.r(r);var s=a(10),e=Object(s.a)({},(function(){var t=this,r=t._self._c;return r("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[r("h1",{attrs:{id:"开发指南"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#开发指南"}},[t._v("#")]),t._v(" 开发指南")]),t._v(" "),r("h2",{attrs:{id:"gone的核心概念"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#gone的核心概念"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/core-concept.html"}},[t._v("Gone的核心概念")])],1),t._v(" "),r("h2",{attrs:{id:"依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#依赖注入"}},[t._v("#")]),t._v(" 依赖注入")]),t._v(" "),r("h3",{attrs:{id:"goner和依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#goner和依赖注入"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./gone-and-inject"}},[t._v("goner和依赖注入")])]),t._v(" "),r("h3",{attrs:{id:"gone支持哪些方式注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#gone支持哪些方式注入"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/goner-inject.html"}},[t._v("Gone支持哪些方式注入?")])],1),t._v(" "),r("h3",{attrs:{id:"对比-gone-和-spring-的依赖注入"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#对比-gone-和-spring-的依赖注入"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/gone-vs-spring.html"}},[t._v("对比 Gone 和 Spring 的依赖注入")])],1),t._v(" "),r("h2",{attrs:{id:"内置模块使用"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#内置模块使用"}},[t._v("#")]),t._v(" 内置模块使用")]),t._v(" "),r("h3",{attrs:{id:"如何优雅使用内置goners"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#如何优雅使用内置goners"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/inner-goner.html"}},[t._v("如何优雅使用内置Goners?")])],1),t._v(" "),r("h3",{attrs:{id:"通过内置goners支持配置文件"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持配置文件"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/config.html"}},[t._v("通过内置Goners支持配置文件")])],1),t._v(" "),r("h3",{attrs:{id:"日志输出"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#日志输出"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/logrus.html"}},[t._v("日志输出")])],1),t._v(" "),r("h3",{attrs:{id:"使用traceid追踪日志"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#使用traceid追踪日志"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/tracer.html"}},[t._v("使用traceId追踪日志")])],1),t._v(" "),r("h3",{attrs:{id:"通过内置goners支持数据库连接"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持数据库连接"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/xorm.html"}},[t._v("通过内置Goners支持数据库连接")])],1),t._v(" "),r("h3",{attrs:{id:"利用redis提供分布式锁和分布式缓存"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#利用redis提供分布式锁和分布式缓存"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./redis"}},[t._v("利用redis提供分布式锁和分布式缓存")])]),t._v(" "),r("h3",{attrs:{id:"用cron表达式配置定时任务"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#用cron表达式配置定时任务"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./schedule"}},[t._v("用cron表达式配置定时任务")])]),t._v(" "),r("h2",{attrs:{id:"效率提升"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#效率提升"}},[t._v("#")]),t._v(" 效率提升")]),t._v(" "),r("h3",{attrs:{id:"自动生成priest"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#自动生成priest"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/auto-gen-priest.html"}},[t._v("自动生成Priest")])],1),t._v(" "),r("h3",{attrs:{id:"hook函数"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#hook函数"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/guide/hooks.html"}},[t._v("Hook函数")])],1),t._v(" "),r("h3",{attrs:{id:"错误处理"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#错误处理"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./error"}},[t._v("错误处理")])]),t._v(" "),r("h3",{attrs:{id:"单元测试"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#单元测试"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"./unit-test"}},[t._v("单元测试")])])])}),[],!1,null,null,null);r.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/51.455395f9.js b/assets/js/50.e18aa89e.js similarity index 99% rename from assets/js/51.455395f9.js rename to assets/js/50.e18aa89e.js index bf9b5c2..8fde772 100644 --- a/assets/js/51.455395f9.js +++ b/assets/js/50.e18aa89e.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[51],{337:function(t,s,a){"use strict";a.r(s);var e=a(10),n=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"自动生成priest"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#自动生成priest"}},[t._v("#")]),t._v(" 自动生成Priest")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#小试牛刀"}},[t._v("小试牛刀")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-安装gone辅助工具"}},[t._v("1. 安装gone辅助工具")])]),s("li",[s("a",{attrs:{href:"#_2-编写goner"}},[t._v("2. 编写Goner")])]),s("li",[s("a",{attrs:{href:"#_3-生成代码"}},[t._v("3. 生成代码")])])])]),s("li",[s("a",{attrs:{href:"#最佳实践"}},[t._v("最佳实践")])])])]),t._v(" "),s("RouterLink",{attrs:{to:"/zh/guide/core-concept.html"}},[t._v("什么是Priest?")]),s("p"),t._v(" "),s("h2",{attrs:{id:"小试牛刀"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#小试牛刀"}},[t._v("#")]),t._v(" 小试牛刀")]),t._v(" "),s("p",[s("code",[t._v("gone priest")]),t._v("命令可以扫描代码中的特殊注释"),s("code",[t._v("//go:gone")]),t._v("生成Priest函数;"),s("code",[t._v("//go:gone")]),t._v("暂时只能用于标记"),s("code",[t._v("func () gone.Goner")]),t._v(" 和 "),s("code",[t._v("func () (gone.Goner, gone.GonerId)")]),t._v("这两种形式的函数,函数必须是公开(以大写字母开头)。")]),t._v(" "),s("h3",{attrs:{id:"_1-安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-安装gone辅助工具"}},[t._v("#")]),t._v(" 1. 安装gone辅助工具")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("gone更多内容参考文档 "),s("RouterLink",{attrs:{to:"/zh/references/gone-tool.html"}},[t._v("gone辅助工具")])],1),t._v(" "),s("h3",{attrs:{id:"_2-编写goner"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-编写goner"}},[t._v("#")]),t._v(" 2. 编写Goner")]),t._v(" "),s("p",[t._v("创建项目,创建文件:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" demo\ngo mod init demo\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" demo.go\n")])])]),s("p",[t._v("编辑demo.go文件代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-生成代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-生成代码"}},[t._v("#")]),t._v(" 3. 生成代码")]),t._v(" "),s("p",[t._v("执行")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone priest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" ./ "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" demo\n")])])]),s("p",[t._v("将生成文件:priest.go\n内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"最佳实践"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#最佳实践"}},[t._v("#")]),t._v(" 最佳实践")]),t._v(" "),s("ul",[s("li",[t._v("创建一个MasterPriest函数,用于Gone框架的启动;")]),t._v(" "),s("li",[t._v("在MasterPriest函数中,调用项目依赖包的Priest函数;")]),t._v(" "),s("li",[t._v("将“gone priest”命令放到MasterPriest函数的"),s("code",[t._v("//go:generate")]),t._v("注释后,例如:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" internal\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:generate gone priest -s . -p $GOPACKAGE -f Priest -o priest.go")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用项目依赖包的Priest函数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用生成的Priest函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("tips: 运行"),s("code",[t._v("go generate ./...")]),t._v(" 会执行 所有"),s("code",[t._v("//go:generate")]),t._v("注释后的命令;")])]),t._v(" "),s("ul",[s("li",[t._v("在MasterPriest函数中,调用调用生成的Priest函数;")]),t._v(" "),s("li",[t._v("git忽略掉生成文件,将生成的文件加入到"),s("code",[t._v(".gitignore")]),t._v("文件中。")])]),t._v(" "),s("p",[t._v("按照上面逻辑,我们编写了一个可供参考的Web空白项目"),s("a",{attrs:{href:"https://github.com/gone-io/examples/tree/main/empty",target:"_blank",rel:"noopener noreferrer"}},[t._v("Demo"),s("OutboundLink")],1),t._v(",已经放到github上,快点击打开吧;另外也可以运行"),s("code",[t._v("gone create web-app")]),t._v("生成一个空白项目,参考"),s("a",{attrs:{href:"https://goner.fun/zh/quick-start/",target:"_blank",rel:"noopener noreferrer"}},[t._v("快速开始\n"),s("OutboundLink")],1),t._v("。")])],1)}),[],!1,null,null,null);s.default=n.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[50],{342:function(t,s,a){"use strict";a.r(s);var e=a(10),n=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"自动生成priest"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#自动生成priest"}},[t._v("#")]),t._v(" 自动生成Priest")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#小试牛刀"}},[t._v("小试牛刀")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-安装gone辅助工具"}},[t._v("1. 安装gone辅助工具")])]),s("li",[s("a",{attrs:{href:"#_2-编写goner"}},[t._v("2. 编写Goner")])]),s("li",[s("a",{attrs:{href:"#_3-生成代码"}},[t._v("3. 生成代码")])])])]),s("li",[s("a",{attrs:{href:"#最佳实践"}},[t._v("最佳实践")])])])]),t._v(" "),s("RouterLink",{attrs:{to:"/zh/guide/core-concept.html"}},[t._v("什么是Priest?")]),s("p"),t._v(" "),s("h2",{attrs:{id:"小试牛刀"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#小试牛刀"}},[t._v("#")]),t._v(" 小试牛刀")]),t._v(" "),s("p",[s("code",[t._v("gone priest")]),t._v("命令可以扫描代码中的特殊注释"),s("code",[t._v("//go:gone")]),t._v("生成Priest函数;"),s("code",[t._v("//go:gone")]),t._v("暂时只能用于标记"),s("code",[t._v("func () gone.Goner")]),t._v(" 和 "),s("code",[t._v("func () (gone.Goner, gone.GonerId)")]),t._v("这两种形式的函数,函数必须是公开(以大写字母开头)。")]),t._v(" "),s("h3",{attrs:{id:"_1-安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-安装gone辅助工具"}},[t._v("#")]),t._v(" 1. 安装gone辅助工具")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("p",[t._v("gone更多内容参考文档 "),s("RouterLink",{attrs:{to:"/zh/references/gone-tool.html"}},[t._v("gone辅助工具")])],1),t._v(" "),s("h3",{attrs:{id:"_2-编写goner"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-编写goner"}},[t._v("#")]),t._v(" 2. 编写Goner")]),t._v(" "),s("p",[t._v("创建项目,创建文件:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" demo\ngo mod init demo\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" demo.go\n")])])]),s("p",[t._v("编辑demo.go文件代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-生成代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-生成代码"}},[t._v("#")]),t._v(" 3. 生成代码")]),t._v(" "),s("p",[t._v("执行")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone priest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" ./ "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" Priest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" priest.go "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" demo\n")])])]),s("p",[t._v("将生成文件:priest.go\n内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Code generated by gone; DO NOT EDIT.")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"最佳实践"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#最佳实践"}},[t._v("#")]),t._v(" 最佳实践")]),t._v(" "),s("ul",[s("li",[t._v("创建一个MasterPriest函数,用于Gone框架的启动;")]),t._v(" "),s("li",[t._v("在MasterPriest函数中,调用项目依赖包的Priest函数;")]),t._v(" "),s("li",[t._v("将“gone priest”命令放到MasterPriest函数的"),s("code",[t._v("//go:generate")]),t._v("注释后,例如:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" internal\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:generate gone priest -s . -p $GOPACKAGE -f Priest -o priest.go")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用项目依赖包的Priest函数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用生成的Priest函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("tips: 运行"),s("code",[t._v("go generate ./...")]),t._v(" 会执行 所有"),s("code",[t._v("//go:generate")]),t._v("注释后的命令;")])]),t._v(" "),s("ul",[s("li",[t._v("在MasterPriest函数中,调用调用生成的Priest函数;")]),t._v(" "),s("li",[t._v("git忽略掉生成文件,将生成的文件加入到"),s("code",[t._v(".gitignore")]),t._v("文件中。")])]),t._v(" "),s("p",[t._v("按照上面逻辑,我们编写了一个可供参考的Web空白项目"),s("a",{attrs:{href:"https://github.com/gone-io/examples/tree/main/empty",target:"_blank",rel:"noopener noreferrer"}},[t._v("Demo"),s("OutboundLink")],1),t._v(",已经放到github上,快点击打开吧;另外也可以运行"),s("code",[t._v("gone create web-app")]),t._v("生成一个空白项目,参考"),s("a",{attrs:{href:"https://goner.fun/zh/quick-start/",target:"_blank",rel:"noopener noreferrer"}},[t._v("快速开始\n"),s("OutboundLink")],1),t._v("。")])],1)}),[],!1,null,null,null);s.default=n.exports}}]); \ No newline at end of file diff --git a/assets/js/52.2b5ddef6.js b/assets/js/51.43361106.js similarity index 99% rename from assets/js/52.2b5ddef6.js rename to assets/js/51.43361106.js index 0a7ad1d..4b9d06b 100644 --- a/assets/js/52.2b5ddef6.js +++ b/assets/js/51.43361106.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[52],{339:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"通过内置goners支持配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持配置文件"}},[t._v("#")]),t._v(" 通过内置Goners支持配置文件")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#举个例子"}},[t._v("举个例子")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-创建mod"}},[t._v("1. 创建mod")])]),s("li",[s("a",{attrs:{href:"#_2-添加配置文件"}},[t._v("2. 添加配置文件")])]),s("li",[s("a",{attrs:{href:"#_3-添加代码"}},[t._v("3. 添加代码")])]),s("li",[s("a",{attrs:{href:"#_4-运行"}},[t._v("4. 运行")])])])]),s("li",[s("a",{attrs:{href:"#配置注入"}},[t._v("配置注入")]),s("ul",[s("li",[s("a",{attrs:{href:"#使用的标签格式"}},[t._v("使用的标签格式")])]),s("li",[s("a",{attrs:{href:"#支持注入的数据类型"}},[t._v("支持注入的数据类型")])]),s("li",[s("a",{attrs:{href:"#配置文件"}},[t._v("配置文件")])])])])])]),s("p"),t._v(" "),s("p",[t._v("在gone中提供了通过内置Goners读取配置文件的方法,配置文件格式暂时只支持"),s("code",[t._v(".properties")]),t._v("。")]),t._v(" "),s("h2",{attrs:{id:"举个例子"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#举个例子"}},[t._v("#")]),t._v(" 举个例子")]),t._v(" "),s("blockquote",[s("p",[t._v("例子的代码源代码可以在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-config",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("找到。")])]),t._v(" "),s("h3",{attrs:{id:"_1-创建mod"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-创建mod"}},[t._v("#")]),t._v(" 1. 创建mod")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod init use-config\n")])])]),s("h3",{attrs:{id:"_2-添加配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-添加配置文件"}},[t._v("#")]),t._v(" 2. 添加配置文件")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" config/default.properties\n")])])]),s("p",[t._v("config/default.properties 文件内容如下:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int8")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("130")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10.222")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("config test")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.bool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("true")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.duration")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10h")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("100x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("200y")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000y")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111y")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-添加代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-添加代码"}},[t._v("#")]),t._v(" 3. 添加代码")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" main.go\n")])])]),s("p",[t._v("main.go 文件内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SubConf "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tX "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"x"`')]),t._v("\n\tY "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"y"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" UseConfig "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v("\n\tprintInt "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针 指向int")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.float64"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.string"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.bool"`')]),t._v("\n\tduration time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.duration"`')]),t._v("\n\tdefaultV "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.default,default=ok"`')]),t._v("\n\n\tsub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SubConf "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.sub"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针,指向结构体")]),t._v("\n\n\tsubs "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("SubConf "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.subs"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//数组")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("UseConfig"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int8=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"printInt=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("printInt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"float64=%f\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string=%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"bool=%t\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"duration=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"defaultV=%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("defaultV"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"sub.x=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("sub"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"subs=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("subs"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("UseConfig"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-运行"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-运行"}},[t._v("#")]),t._v(" 4. 运行")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("运行结果如下:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("int=10\nint8=-126\nprintInt=130\nfloat64=10.222000\nstring=config test\nbool=true\nduration=10h0m0s\ndefaultV=ok\nsub.x=&{100x 200y}\nsubs=[{0000x 0000y} {1111x 1111y}]\n")])])]),s("h2",{attrs:{id:"配置注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置注入"}},[t._v("#")]),t._v(" 配置注入")]),t._v(" "),s("p",[t._v("从例子中可以看到,配置项也可以通过“依赖注入”到Goner。")]),t._v(" "),s("h3",{attrs:{id:"使用的标签格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用的标签格式"}},[t._v("#")]),t._v(" 使用的标签格式")]),t._v(" "),s("p",[t._v("配置注入的标签格式如下:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v('gone:"config,${key},default=${defaultValue}"\n')])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("其中含义如下:")]),t._v(" "),s("ol",[s("li",[t._v("标签名依然为"),s("code",[t._v("gone")])]),t._v(" "),s("li",[s("code",[t._v("config")]),t._v("为固定值,表示该属性为配置")]),t._v(" "),s("li",[s("code",[t._v("${key}")]),t._v("为配置在配置文件中的key")]),t._v(" "),s("li",[s("code",[t._v("default=${defaultValue}")]),t._v("用于指定默认值,可以省略;"),s("code",[t._v("${defaultValue}")]),t._v("为默认值;如果没有指定默认值;如果配置文件中缺少对应key,则会报错")])])]),t._v(" "),s("h3",{attrs:{id:"支持注入的数据类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的数据类型"}},[t._v("#")]),t._v(" 支持注入的数据类型")]),t._v(" "),s("blockquote",[s("p",[t._v("在 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#goner-%E9%80%9D%E8%80%85",target:"_blank",rel:"noopener noreferrer"}},[t._v("核心概念-Goner"),s("OutboundLink")],1),t._v("中,我们讲到Goner可以被注入到其他结构体的属性上;这里,配置作为一种特殊情况,也可以被注入到其他结构体的属性上,他的实现是通过的"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#%F0%9F%A7%9B%F0%9F%8F%BB%E2%80%8D%E2%99%80%EF%B8%8Fvampire-%E5%90%B8%E8%A1%80%E9%AC%BC",target:"_blank",rel:"noopener noreferrer"}},[t._v("Vampire(吸血鬼)"),s("OutboundLink")],1),t._v(",如果感觉兴趣,可以看"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.5/goner/config",target:"_blank",rel:"noopener noreferrer"}},[t._v("这部分源代码"),s("OutboundLink")],1),t._v("。")])]),t._v(" "),s("p",[t._v("属性支持的类型,列举如下:")]),t._v(" "),s("ol",[s("li",[t._v("基础类型\n"),s("ul",[s("li",[t._v("布尔型:bool")]),t._v(" "),s("li",[t._v("整数类型:int, int64, int32, int16, int8")]),t._v(" "),s("li",[t._v("非负整数类型:uint, uint64, uint32, uint16, uint8")]),t._v(" "),s("li",[t._v("浮点型:float64, float32")]),t._v(" "),s("li",[t._v("字符串类型:string")])])])]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("整数类型和非负整数类型,需要注意数据结构表示的数的范围,数字过大可能会溢出;比如"),s("code",[t._v("int8")]),t._v("表示的范围是"),s("code",[t._v("-128")]),t._v(" ~ "),s("code",[t._v("127")]),t._v(",如果用其接收一个大于127的配置就会发生溢出(配置128将得到-128)。")])]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("time.Duration\n为方便时间的解析,支持了"),s("code",[t._v("time.Duration")]),t._v("类型的配置,可以使用下面的单位:\n"),s("ul",[s("li",[t._v("ns 纳秒")]),t._v(" "),s("li",[t._v("us 微秒")]),t._v(" "),s("li",[t._v("ms 毫秒")]),t._v(" "),s("li",[t._v("s 秒")]),t._v(" "),s("li",[t._v("m 分钟")]),t._v(" "),s("li",[t._v("h 小时")])])])]),t._v(" "),s("p",[t._v("在例子中,"),s("code",[t._v("my.conf.duration=10h")]),t._v("单位为h,表示10个小时;"),s("code",[t._v("1h10m10s")]),t._v("则表示1小时10分10秒。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("实现采用的"),s("code",[t._v("time.ParseDuration")]),t._v(",所以可以参考:"),s("a",{attrs:{href:"https://pkg.go.dev/time#ParseDuration",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://pkg.go.dev/time#ParseDuration"),s("OutboundLink")],1)])]),t._v(" "),s("ol",{attrs:{start:"3"}},[s("li",[t._v("结构体类型\n在例子中,给出了配置结构体的类型,需要满足如下规则:\n"),s("ul",[s("li",[t._v("结构体中需要配置的属性为公开的,即大写字母打头")]),t._v(" "),s("li",[t._v("使用"),s("code",[t._v("properties")]),t._v("标签指定属性的名字")]),t._v(" "),s("li",[t._v("支持嵌套")])])])]),t._v(" "),s("p",[t._v("说明:在例子中"),s("code",[t._v("UseConfig")]),t._v("的属性"),s("code",[t._v("sub")]),t._v("要读取的配置key为"),s("code",[t._v("my.conf.sub")]),t._v(";"),s("code",[t._v("sub")]),t._v("的类型为"),s("code",[t._v("SubConf")]),t._v(",标注了属性"),s("code",[t._v("X")]),t._v("的配置名为"),s("code",[t._v('properties:"x"')]),t._v(",那么"),s("code",[t._v("sub.X")]),t._v("的值将读取配置项"),s("code",[t._v("my.conf.sub.x")]),t._v("。")]),t._v(" "),s("ol",{attrs:{start:"4"}},[s("li",[t._v("数组类型\n配置的注入支持Slice类型,Slice的元素目前只支持结构体、结构体的指针;配置的key形式如下:")])]),t._v(" "),s("div",{staticClass:"language-ini extra-class"},[s("pre",{pre:!0,attrs:{class:"language-ini"}},[s("code",[t._v("${injectConfigkey}[${index}].${structAttributeName}\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("说明")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("${injectConfigkey}")]),t._v("为待注入项的key")]),t._v(" "),s("li",[s("code",[t._v("${index}")]),t._v("为数组下标")]),t._v(" "),s("li",[s("code",[t._v("${structAttributeName}")]),t._v("为被注入结构体的属性上的"),s("code",[t._v("properties")]),t._v("标注的值")])])]),t._v(" "),s("ol",{attrs:{start:"5"}},[s("li",[t._v("指针类型\n支持被注入属性的类型为指针。")])]),t._v(" "),s("h3",{attrs:{id:"配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置文件"}},[t._v("#")]),t._v(" 配置文件")]),t._v(" "),s("h4",{attrs:{id:"配置文件目录"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置文件目录"}},[t._v("#")]),t._v(" 配置文件目录")]),t._v(" "),s("p",[t._v("配置文件存放的目录由三部分决定:")]),t._v(" "),s("ol",[s("li",[t._v("可执行文件和程序当前工作路径")]),t._v(" "),s("li",[t._v("相对配置目录\n相对配置目录默认是"),s("code",[t._v("config")]),t._v(",可以通过启动传参"),s("code",[t._v("--conf $configDir")]),t._v("来改变")])]),t._v(" "),s("p",[t._v("举个例子,如果编译后的gone程序保存为"),s("code",[t._v("/app/gone-app")]),t._v(",我们进入"),s("code",[t._v("/home/degfy")]),t._v("目录运行:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" /home/degfy/\n/app/gone-app\n")])])]),s("p",[t._v("并没有通过传参修改相对配置目录,那么该程序的配置目录为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config")]),t._v(" "),s("li",[t._v("/home/degfy/config")])]),t._v(" "),s("h4",{attrs:{id:"默认配置文件路径"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#默认配置文件路径"}},[t._v("#")]),t._v(" 默认配置文件路径")]),t._v(" "),s("p",[t._v("沿用前面的例子,配置文件的路径为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config/default.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/default.properties\n这个两个配置文件中的配置为默认配置,是不随环境改变而改变的。")])]),t._v(" "),s("h4",{attrs:{id:"环境相关配置文件路径"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#环境相关配置文件路径"}},[t._v("#")]),t._v(" 环境相关配置文件路径")]),t._v(" "),s("p",[t._v("开发过程中,我们一般会分环境配置,一般情况会分为:")]),t._v(" "),s("ul",[s("li",[t._v("local 本地开发环境")]),t._v(" "),s("li",[t._v("dev 线上开发环境")]),t._v(" "),s("li",[t._v("test 线上测试环境")]),t._v(" "),s("li",[t._v("prod 生成环境")])]),t._v(" "),s("p",[t._v("我们可以通过环境变量"),s("code",[t._v("ENV")]),t._v("或者在启动时传参"),s("code",[t._v("--nev $env")]),t._v("来指定当前环境,如果都没有设置,默认环境是"),s("code",[t._v("local")]),t._v("。\n沿用前面的例子,与环境相关的配置文件为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config/local.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),s("h4",{attrs:{id:"配置加载的顺序"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置加载的顺序"}},[t._v("#")]),t._v(" 配置加载的顺序")]),t._v(" "),s("p",[t._v("从前面,我们可以得知我们启动时会存在4个配置文件,他们的加载顺序如下:")]),t._v(" "),s("ol",[s("li",[t._v("/app/config/default.properties")]),t._v(" "),s("li",[t._v("/app/config/local.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/default.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),s("p",[t._v("如果这些配置文件中存在相同的配置项,后加载的值会先加载的值。也就是环境相关的配置会覆盖默认的配置,当前运行路径的配置会覆盖程序所在目录的配置。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("最佳实践")]),t._v(" "),s("ol",[s("li",[t._v("将所有配置放到默认配置文件中;在环境相关配置文件中根据环境的需要进行覆盖;")]),t._v(" "),s("li",[t._v("配置的设计应该尽量少,推荐采用“约定优配置”思路来进行设计,配置可以考虑赋予默认值;")]),t._v(" "),s("li",[t._v("为了使代码“开箱可用”,推荐在local.properties中设置完整的配置,让程序不需要任何其他的配置就能运行,让其他小伙伴clone代码后就能丝滑启动程序。")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[51],{338:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"通过内置goners支持配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持配置文件"}},[t._v("#")]),t._v(" 通过内置Goners支持配置文件")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#举个例子"}},[t._v("举个例子")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-创建mod"}},[t._v("1. 创建mod")])]),s("li",[s("a",{attrs:{href:"#_2-添加配置文件"}},[t._v("2. 添加配置文件")])]),s("li",[s("a",{attrs:{href:"#_3-添加代码"}},[t._v("3. 添加代码")])]),s("li",[s("a",{attrs:{href:"#_4-运行"}},[t._v("4. 运行")])])])]),s("li",[s("a",{attrs:{href:"#配置注入"}},[t._v("配置注入")]),s("ul",[s("li",[s("a",{attrs:{href:"#使用的标签格式"}},[t._v("使用的标签格式")])]),s("li",[s("a",{attrs:{href:"#支持注入的数据类型"}},[t._v("支持注入的数据类型")])]),s("li",[s("a",{attrs:{href:"#配置文件"}},[t._v("配置文件")])])])])])]),s("p"),t._v(" "),s("p",[t._v("在gone中提供了通过内置Goners读取配置文件的方法,配置文件格式暂时只支持"),s("code",[t._v(".properties")]),t._v("。")]),t._v(" "),s("h2",{attrs:{id:"举个例子"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#举个例子"}},[t._v("#")]),t._v(" 举个例子")]),t._v(" "),s("blockquote",[s("p",[t._v("例子的代码源代码可以在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-config",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("找到。")])]),t._v(" "),s("h3",{attrs:{id:"_1-创建mod"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-创建mod"}},[t._v("#")]),t._v(" 1. 创建mod")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod init use-config\n")])])]),s("h3",{attrs:{id:"_2-添加配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-添加配置文件"}},[t._v("#")]),t._v(" 2. 添加配置文件")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" config/default.properties\n")])])]),s("p",[t._v("config/default.properties 文件内容如下:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.int8")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("130")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10.222")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("config test")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.bool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("true")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.duration")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("10h")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("100x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.sub.y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("200y")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[0].y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("0000y")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].x")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111x")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("my.conf.subs[1].y")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("1111y")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-添加代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-添加代码"}},[t._v("#")]),t._v(" 3. 添加代码")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("touch")]),t._v(" main.go\n")])])]),s("p",[t._v("main.go 文件内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SubConf "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tX "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"x"`')]),t._v("\n\tY "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`properties:"y"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" UseConfig "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v("\n\tprintInt "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.int8"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针 指向int")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.float64"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.string"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.bool"`')]),t._v("\n\tduration time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.duration"`')]),t._v("\n\tdefaultV "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.default,default=ok"`')]),t._v("\n\n\tsub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SubConf "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.sub"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针,指向结构体")]),t._v("\n\n\tsubs "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("SubConf "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,my.conf.subs"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//数组")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("UseConfig"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"int8=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int8")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"printInt=%d\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("printInt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"float64=%f\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"string=%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"bool=%t\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("bool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"duration=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"defaultV=%s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("defaultV"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"sub.x=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("sub"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"subs=%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" g"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("subs"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("UseConfig"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_4-运行"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_4-运行"}},[t._v("#")]),t._v(" 4. 运行")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("运行结果如下:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("int=10\nint8=-126\nprintInt=130\nfloat64=10.222000\nstring=config test\nbool=true\nduration=10h0m0s\ndefaultV=ok\nsub.x=&{100x 200y}\nsubs=[{0000x 0000y} {1111x 1111y}]\n")])])]),s("h2",{attrs:{id:"配置注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置注入"}},[t._v("#")]),t._v(" 配置注入")]),t._v(" "),s("p",[t._v("从例子中可以看到,配置项也可以通过“依赖注入”到Goner。")]),t._v(" "),s("h3",{attrs:{id:"使用的标签格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用的标签格式"}},[t._v("#")]),t._v(" 使用的标签格式")]),t._v(" "),s("p",[t._v("配置注入的标签格式如下:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v('gone:"config,${key},default=${defaultValue}"\n')])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("其中含义如下:")]),t._v(" "),s("ol",[s("li",[t._v("标签名依然为"),s("code",[t._v("gone")])]),t._v(" "),s("li",[s("code",[t._v("config")]),t._v("为固定值,表示该属性为配置")]),t._v(" "),s("li",[s("code",[t._v("${key}")]),t._v("为配置在配置文件中的key")]),t._v(" "),s("li",[s("code",[t._v("default=${defaultValue}")]),t._v("用于指定默认值,可以省略;"),s("code",[t._v("${defaultValue}")]),t._v("为默认值;如果没有指定默认值;如果配置文件中缺少对应key,则会报错")])])]),t._v(" "),s("h3",{attrs:{id:"支持注入的数据类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的数据类型"}},[t._v("#")]),t._v(" 支持注入的数据类型")]),t._v(" "),s("blockquote",[s("p",[t._v("在 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#goner-%E9%80%9D%E8%80%85",target:"_blank",rel:"noopener noreferrer"}},[t._v("核心概念-Goner"),s("OutboundLink")],1),t._v("中,我们讲到Goner可以被注入到其他结构体的属性上;这里,配置作为一种特殊情况,也可以被注入到其他结构体的属性上,他的实现是通过的"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#%F0%9F%A7%9B%F0%9F%8F%BB%E2%80%8D%E2%99%80%EF%B8%8Fvampire-%E5%90%B8%E8%A1%80%E9%AC%BC",target:"_blank",rel:"noopener noreferrer"}},[t._v("Vampire(吸血鬼)"),s("OutboundLink")],1),t._v(",如果感觉兴趣,可以看"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/v0.1.5/goner/config",target:"_blank",rel:"noopener noreferrer"}},[t._v("这部分源代码"),s("OutboundLink")],1),t._v("。")])]),t._v(" "),s("p",[t._v("属性支持的类型,列举如下:")]),t._v(" "),s("ol",[s("li",[t._v("基础类型\n"),s("ul",[s("li",[t._v("布尔型:bool")]),t._v(" "),s("li",[t._v("整数类型:int, int64, int32, int16, int8")]),t._v(" "),s("li",[t._v("非负整数类型:uint, uint64, uint32, uint16, uint8")]),t._v(" "),s("li",[t._v("浮点型:float64, float32")]),t._v(" "),s("li",[t._v("字符串类型:string")])])])]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("整数类型和非负整数类型,需要注意数据结构表示的数的范围,数字过大可能会溢出;比如"),s("code",[t._v("int8")]),t._v("表示的范围是"),s("code",[t._v("-128")]),t._v(" ~ "),s("code",[t._v("127")]),t._v(",如果用其接收一个大于127的配置就会发生溢出(配置128将得到-128)。")])]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("time.Duration\n为方便时间的解析,支持了"),s("code",[t._v("time.Duration")]),t._v("类型的配置,可以使用下面的单位:\n"),s("ul",[s("li",[t._v("ns 纳秒")]),t._v(" "),s("li",[t._v("us 微秒")]),t._v(" "),s("li",[t._v("ms 毫秒")]),t._v(" "),s("li",[t._v("s 秒")]),t._v(" "),s("li",[t._v("m 分钟")]),t._v(" "),s("li",[t._v("h 小时")])])])]),t._v(" "),s("p",[t._v("在例子中,"),s("code",[t._v("my.conf.duration=10h")]),t._v("单位为h,表示10个小时;"),s("code",[t._v("1h10m10s")]),t._v("则表示1小时10分10秒。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("实现采用的"),s("code",[t._v("time.ParseDuration")]),t._v(",所以可以参考:"),s("a",{attrs:{href:"https://pkg.go.dev/time#ParseDuration",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://pkg.go.dev/time#ParseDuration"),s("OutboundLink")],1)])]),t._v(" "),s("ol",{attrs:{start:"3"}},[s("li",[t._v("结构体类型\n在例子中,给出了配置结构体的类型,需要满足如下规则:\n"),s("ul",[s("li",[t._v("结构体中需要配置的属性为公开的,即大写字母打头")]),t._v(" "),s("li",[t._v("使用"),s("code",[t._v("properties")]),t._v("标签指定属性的名字")]),t._v(" "),s("li",[t._v("支持嵌套")])])])]),t._v(" "),s("p",[t._v("说明:在例子中"),s("code",[t._v("UseConfig")]),t._v("的属性"),s("code",[t._v("sub")]),t._v("要读取的配置key为"),s("code",[t._v("my.conf.sub")]),t._v(";"),s("code",[t._v("sub")]),t._v("的类型为"),s("code",[t._v("SubConf")]),t._v(",标注了属性"),s("code",[t._v("X")]),t._v("的配置名为"),s("code",[t._v('properties:"x"')]),t._v(",那么"),s("code",[t._v("sub.X")]),t._v("的值将读取配置项"),s("code",[t._v("my.conf.sub.x")]),t._v("。")]),t._v(" "),s("ol",{attrs:{start:"4"}},[s("li",[t._v("数组类型\n配置的注入支持Slice类型,Slice的元素目前只支持结构体、结构体的指针;配置的key形式如下:")])]),t._v(" "),s("div",{staticClass:"language-ini extra-class"},[s("pre",{pre:!0,attrs:{class:"language-ini"}},[s("code",[t._v("${injectConfigkey}[${index}].${structAttributeName}\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("说明")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("${injectConfigkey}")]),t._v("为待注入项的key")]),t._v(" "),s("li",[s("code",[t._v("${index}")]),t._v("为数组下标")]),t._v(" "),s("li",[s("code",[t._v("${structAttributeName}")]),t._v("为被注入结构体的属性上的"),s("code",[t._v("properties")]),t._v("标注的值")])])]),t._v(" "),s("ol",{attrs:{start:"5"}},[s("li",[t._v("指针类型\n支持被注入属性的类型为指针。")])]),t._v(" "),s("h3",{attrs:{id:"配置文件"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置文件"}},[t._v("#")]),t._v(" 配置文件")]),t._v(" "),s("h4",{attrs:{id:"配置文件目录"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置文件目录"}},[t._v("#")]),t._v(" 配置文件目录")]),t._v(" "),s("p",[t._v("配置文件存放的目录由三部分决定:")]),t._v(" "),s("ol",[s("li",[t._v("可执行文件和程序当前工作路径")]),t._v(" "),s("li",[t._v("相对配置目录\n相对配置目录默认是"),s("code",[t._v("config")]),t._v(",可以通过启动传参"),s("code",[t._v("--conf $configDir")]),t._v("来改变")])]),t._v(" "),s("p",[t._v("举个例子,如果编译后的gone程序保存为"),s("code",[t._v("/app/gone-app")]),t._v(",我们进入"),s("code",[t._v("/home/degfy")]),t._v("目录运行:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" /home/degfy/\n/app/gone-app\n")])])]),s("p",[t._v("并没有通过传参修改相对配置目录,那么该程序的配置目录为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config")]),t._v(" "),s("li",[t._v("/home/degfy/config")])]),t._v(" "),s("h4",{attrs:{id:"默认配置文件路径"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#默认配置文件路径"}},[t._v("#")]),t._v(" 默认配置文件路径")]),t._v(" "),s("p",[t._v("沿用前面的例子,配置文件的路径为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config/default.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/default.properties\n这个两个配置文件中的配置为默认配置,是不随环境改变而改变的。")])]),t._v(" "),s("h4",{attrs:{id:"环境相关配置文件路径"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#环境相关配置文件路径"}},[t._v("#")]),t._v(" 环境相关配置文件路径")]),t._v(" "),s("p",[t._v("开发过程中,我们一般会分环境配置,一般情况会分为:")]),t._v(" "),s("ul",[s("li",[t._v("local 本地开发环境")]),t._v(" "),s("li",[t._v("dev 线上开发环境")]),t._v(" "),s("li",[t._v("test 线上测试环境")]),t._v(" "),s("li",[t._v("prod 生成环境")])]),t._v(" "),s("p",[t._v("我们可以通过环境变量"),s("code",[t._v("ENV")]),t._v("或者在启动时传参"),s("code",[t._v("--nev $env")]),t._v("来指定当前环境,如果都没有设置,默认环境是"),s("code",[t._v("local")]),t._v("。\n沿用前面的例子,与环境相关的配置文件为:")]),t._v(" "),s("ul",[s("li",[t._v("/app/config/local.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),s("h4",{attrs:{id:"配置加载的顺序"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置加载的顺序"}},[t._v("#")]),t._v(" 配置加载的顺序")]),t._v(" "),s("p",[t._v("从前面,我们可以得知我们启动时会存在4个配置文件,他们的加载顺序如下:")]),t._v(" "),s("ol",[s("li",[t._v("/app/config/default.properties")]),t._v(" "),s("li",[t._v("/app/config/local.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/default.properties")]),t._v(" "),s("li",[t._v("/home/degfy/config/local.properties")])]),t._v(" "),s("p",[t._v("如果这些配置文件中存在相同的配置项,后加载的值会先加载的值。也就是环境相关的配置会覆盖默认的配置,当前运行路径的配置会覆盖程序所在目录的配置。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("最佳实践")]),t._v(" "),s("ol",[s("li",[t._v("将所有配置放到默认配置文件中;在环境相关配置文件中根据环境的需要进行覆盖;")]),t._v(" "),s("li",[t._v("配置的设计应该尽量少,推荐采用“约定优配置”思路来进行设计,配置可以考虑赋予默认值;")]),t._v(" "),s("li",[t._v("为了使代码“开箱可用”,推荐在local.properties中设置完整的配置,让程序不需要任何其他的配置就能运行,让其他小伙伴clone代码后就能丝滑启动程序。")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/53.5b742bbf.js b/assets/js/52.4bef3c38.js similarity index 99% rename from assets/js/53.5b742bbf.js rename to assets/js/52.4bef3c38.js index b4ec661..1939b26 100644 --- a/assets/js/53.5b742bbf.js +++ b/assets/js/52.4bef3c38.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[53],{342:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone的核心概念"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone的核心概念"}},[t._v("#")]),t._v(" Gone的核心概念")]),t._v(" "),s("p",[t._v("“我们编写的代码,终究只是死物,除非他在"),s("strong",[t._v("天国")]),t._v("被"),s("strong",[t._v("复活")]),t._v(",为此我们需要将他"),s("strong",[t._v("埋葬")]),t._v("在"),s("strong",[t._v("墓园")]),t._v("。”")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#goner-逝者"}},[t._v("Goner(逝者)")]),s("ul",[s("li",[s("a",{attrs:{href:"#🔮prophet-先知"}},[t._v("🔮Prophet(先知)")])]),s("li",[s("a",{attrs:{href:"#😇angel-天使"}},[t._v("😇Angel(天使)")])]),s("li",[s("a",{attrs:{href:"#🧛🏻‍♀️vampire-吸血鬼"}},[t._v("🧛🏻‍♀️Vampire(吸血鬼)")])])])]),s("li",[s("a",{attrs:{href:"#cemetery-墓园"}},[t._v("Cemetery(墓园)")]),s("ul",[s("li",[s("a",{attrs:{href:"#bury-埋葬"}},[t._v("Bury(埋葬)")])]),s("li",[s("a",{attrs:{href:"#revive-复活"}},[t._v("Revive(复活)")])])])]),s("li",[s("a",{attrs:{href:"#heaven-天国"}},[t._v("Heaven(天国)")])]),s("li",[s("a",{attrs:{href:"#priest-牧师"}},[t._v("Priest (牧师)")])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"goner-逝者"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner-逝者"}},[t._v("#")]),t._v(" Goner(逝者)")]),t._v(" "),s("p",[t._v("在Gone框架中,最基本的和最核心的概念就是 Goner,"),s("strong",[t._v("Goner 是指的匿名嵌入了 "),s("code",[t._v("gone.Flag")]),t._v(" 的结构体")]),t._v("。举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Goner是Gone框架中的组件,是实现依赖注入的关键:")]),t._v(" "),s("ol",[s("li",[t._v("Goner可以作为属性被注入到其他结构体")]),t._v(" "),s("li",[t._v("Goner的属性可以被其他类型注入")])]),t._v(" "),s("p",[t._v("为什么需要内嵌一个 "),s("code",[t._v("gone.Flag")]),t._v("?是因为我们希望限制依赖注入的范围,让依赖注入只发生在Goners之间,让Gone框架的组件实现有一个统一的模式。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("下面是Goner和gone.Flag的源代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Flag "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Flag"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner 逝者")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Goner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Goner作为接口,要求实现它的“对象”拥有一个私有的方法"),s("code",[t._v("goneFlag()")]),t._v(";由于go语言可见性的限制,不能在"),s("code",[t._v("github.com/gone-io/gone")]),t._v("以外的包中实现其内部定义的私有方法;一个结构体要成为Goner,只能通过内嵌"),s("code",[t._v("gone.Flag")]),t._v("才能“继承”私有的方法"),s("code",[t._v("goneFlag()")]),t._v("。这段话可能有些不好理解,其实就是说Goner的实现只能通过内嵌gone.Flag来完成,不能通过实现"),s("code",[t._v("goneFlag()")]),t._v("方法来完成。")])]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("多个"),s("strong",[t._v("Goner")]),t._v(",我们使用其复数形式("),s("strong",[t._v("Goners")]),t._v(")表示。")])]),t._v(" "),s("p",[t._v("在Gone框架中,还包含了三类特殊的Goner,定义如下:")]),t._v(" "),s("h3",{attrs:{id:"🔮prophet-先知"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#🔮prophet-先知"}},[t._v("#")]),t._v(" 🔮Prophet(先知)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",在普通 "),s("strong",[t._v("Goner")]),t._v(" 上实现了 "),s("strong",[s("code",[t._v("AfterRevive() AfterReviveError")])]),t._v(" 方法就是 "),s("strong",[t._v("Prophet(先知)")]),t._v(";"),s("strong",[t._v("AfterRevive")]),t._v(" 会在 "),s("strong",[t._v("Goner")]),t._v(" 被复活后被执行。")]),t._v(" "),s("p",[t._v("Prophet接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Prophet 先知")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Prophet "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//AfterRevive 在Goner复活后会被执行")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" AfterReviveError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"😇angel-天使"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#😇angel-天使"}},[t._v("#")]),t._v(" 😇Angel(天使)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",拥有天使左翼"),s("code",[t._v("Start(Cemetery) error")]),t._v(" 和 天使右翼"),s("code",[t._v("Stop(Cemetery) error")]),t._v(",左翼负责生(用于分配资源,启动某项服务),右翼负责死(用于终止某项服务,回收资源)。")]),t._v(" "),s("p",[t._v("Angel接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Angel "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stop")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"🧛🏻‍♀️vampire-吸血鬼"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#🧛🏻‍♀️vampire-吸血鬼"}},[t._v("#")]),t._v(" 🧛🏻‍♀️Vampire(吸血鬼)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",拥有特殊能力——吸血"),s("code",[t._v("Suck(conf string, v reflect.Value) SuckError")]),t._v("。"),s("strong",[t._v("Suck")]),t._v("可以将不是"),s("strong",[t._v("Goner")]),t._v("的值赋予注入给Goner属性。")]),t._v(" "),s("p",[t._v("Vampire接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SuckError "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Vampire "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Suck")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v reflect"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" SuckError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"cemetery-墓园"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#cemetery-墓园"}},[t._v("#")]),t._v(" Cemetery(墓园)")]),t._v(" "),s("p",[t._v("Cemetery用于管理Goners,主要提供Bury(埋葬)和 revive(复活)的方法,其接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cemetery "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ... 其他方法")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Cemetery "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 埋葬,将逝者埋葬到墓园")]),t._v("\n\n\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//ReviveAllFromTombs 复活所有Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReviveAllFromTombs")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("从代码上可以看到Cemetery本身也是一个Goner,在Gone框架启动时他会被自动埋葬和复活。")]),t._v(" "),s("h3",{attrs:{id:"bury-埋葬"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#bury-埋葬"}},[t._v("#")]),t._v(" Bury(埋葬)")]),t._v(" "),s("p",[t._v("将Goner "),s("strong",[t._v("埋葬")]),t._v(" 到 "),s("strong",[t._v("Cemetery")]),t._v(" 就是将Goner注册到框架,以待后续完成属性的注入;在代码实现上,"),s("strong",[t._v("Bury")]),t._v("是"),s("strong",[t._v("Cemetery")]),t._v("上的公开方法,一般在通过 "),s("strong",[t._v("Priest")]),t._v(" 函数调用该方法。")]),t._v(" "),s("h3",{attrs:{id:"revive-复活"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#revive-复活"}},[t._v("#")]),t._v(" Revive(复活)")]),t._v(" "),s("p",[t._v("Revive(复活)指的是Goner所有需要注入的属性完成注入的过程。在函数"),s("code",[t._v("ReviveAllFromTombs() error")]),t._v("中,所有被"),s("strong",[t._v("埋葬")]),t._v("到"),s("strong",[t._v("Cemetery")]),t._v("的Goners都会被尝试复活,如果有属性不能正常注入,程序将panic。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[s("strong",[t._v("ReviveAllFromTombs")]),t._v("在完成了复活所有的"),s("strong",[t._v("Goners")]),t._v("后,会调用所有"),s("strong",[t._v("Prophet")]),t._v("的 "),s("strong",[t._v("AfterRevive")]),t._v("方法。")])]),t._v(" "),s("h2",{attrs:{id:"heaven-天国"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#heaven-天国"}},[t._v("#")]),t._v(" Heaven(天国)")]),t._v(" "),s("p",[t._v("Heaven(天国)代表了一个Gone程序,用于管理程序的启动、停止等状态和流程(复活在启动前完成),用于在启动前后以及程序停止前执行一些hook任务。Heaven接收一个牧师函数开始运行,例如:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用 cemetery.Bury 埋葬 Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者 调用其他 Priest 函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("或者:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用 cemetery.Bury 埋葬 Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者 调用其他 Priest 函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TODO: 启动后执行一些操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"priest-牧师"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#priest-牧师"}},[t._v("#")]),t._v(" Priest (牧师)")]),t._v(" "),s("p",[t._v("Priest (牧师)是负责将"),s("strong",[t._v("Goner")]),t._v("埋葬到"),s("strong",[t._v("Cemetery")]),t._v("的函数,他的定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Priest "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])]),s("p",[t._v("在"),s("strong",[t._v("Priest")]),t._v("函数实现上,可以调用 "),s("strong",[t._v("cemetery.Bury")]),t._v(" 来完成,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名埋葬,不指定被埋葬Goner的GonerId")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"X"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("也可以通过调用其他的"),s("strong",[t._v("Priest")]),t._v("函数来完成:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("如果我们开发了一个组件包,其中使用了多个"),s("strong",[t._v("Goners")]),t._v("来完成响应的功能,使用时需要同时"),s("strong",[t._v("埋葬")]),t._v("这些"),s("strong",[t._v("Goners")]),t._v(",那么我们可以编写一个"),s("strong",[t._v("Priest")]),t._v("函数方便业务代码批量"),s("strong",[t._v("埋葬")]),t._v("这些"),s("strong",[t._v("Goners")]),t._v("。")]),t._v(" "),s("p",[t._v("框架内置的组件包,我们就是这样干的,"),s("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("代码"),s("OutboundLink")],1),t._v(" 和 "),s("a",{attrs:{href:"https://goner.fun/zh/goners/#%E6%A1%86%E6%9E%B6%E5%86%85%E7%BD%AEgoners",target:"_blank",rel:"noopener noreferrer"}},[t._v("文档"),s("OutboundLink")],1),t._v("。")]),t._v(" "),s("p",[t._v("另外,我们开发了一个命令行辅助工具"),s("strong",[t._v("gone")]),t._v(",用于扫描特殊注释"),s("code",[t._v("//go:gone")]),t._v("自动生成牧师函数,参考"),s("RouterLink",{attrs:{to:"/zh/guide/auto-gen-priest.html"}},[t._v("自动生成Priest")]),t._v("。")],1)])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[52],{339:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone的核心概念"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone的核心概念"}},[t._v("#")]),t._v(" Gone的核心概念")]),t._v(" "),s("p",[t._v("“我们编写的代码,终究只是死物,除非他在"),s("strong",[t._v("天国")]),t._v("被"),s("strong",[t._v("复活")]),t._v(",为此我们需要将他"),s("strong",[t._v("埋葬")]),t._v("在"),s("strong",[t._v("墓园")]),t._v("。”")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#goner-逝者"}},[t._v("Goner(逝者)")]),s("ul",[s("li",[s("a",{attrs:{href:"#🔮prophet-先知"}},[t._v("🔮Prophet(先知)")])]),s("li",[s("a",{attrs:{href:"#😇angel-天使"}},[t._v("😇Angel(天使)")])]),s("li",[s("a",{attrs:{href:"#🧛🏻‍♀️vampire-吸血鬼"}},[t._v("🧛🏻‍♀️Vampire(吸血鬼)")])])])]),s("li",[s("a",{attrs:{href:"#cemetery-墓园"}},[t._v("Cemetery(墓园)")]),s("ul",[s("li",[s("a",{attrs:{href:"#bury-埋葬"}},[t._v("Bury(埋葬)")])]),s("li",[s("a",{attrs:{href:"#revive-复活"}},[t._v("Revive(复活)")])])])]),s("li",[s("a",{attrs:{href:"#heaven-天国"}},[t._v("Heaven(天国)")])]),s("li",[s("a",{attrs:{href:"#priest-牧师"}},[t._v("Priest (牧师)")])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"goner-逝者"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner-逝者"}},[t._v("#")]),t._v(" Goner(逝者)")]),t._v(" "),s("p",[t._v("在Gone框架中,最基本的和最核心的概念就是 Goner,"),s("strong",[t._v("Goner 是指的匿名嵌入了 "),s("code",[t._v("gone.Flag")]),t._v(" 的结构体")]),t._v("。举个例子:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Goner是Gone框架中的组件,是实现依赖注入的关键:")]),t._v(" "),s("ol",[s("li",[t._v("Goner可以作为属性被注入到其他结构体")]),t._v(" "),s("li",[t._v("Goner的属性可以被其他类型注入")])]),t._v(" "),s("p",[t._v("为什么需要内嵌一个 "),s("code",[t._v("gone.Flag")]),t._v("?是因为我们希望限制依赖注入的范围,让依赖注入只发生在Goners之间,让Gone框架的组件实现有一个统一的模式。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("下面是Goner和gone.Flag的源代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Flag "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Flag"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner 逝者")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Goner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("Goner作为接口,要求实现它的“对象”拥有一个私有的方法"),s("code",[t._v("goneFlag()")]),t._v(";由于go语言可见性的限制,不能在"),s("code",[t._v("github.com/gone-io/gone")]),t._v("以外的包中实现其内部定义的私有方法;一个结构体要成为Goner,只能通过内嵌"),s("code",[t._v("gone.Flag")]),t._v("才能“继承”私有的方法"),s("code",[t._v("goneFlag()")]),t._v("。这段话可能有些不好理解,其实就是说Goner的实现只能通过内嵌gone.Flag来完成,不能通过实现"),s("code",[t._v("goneFlag()")]),t._v("方法来完成。")])]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("多个"),s("strong",[t._v("Goner")]),t._v(",我们使用其复数形式("),s("strong",[t._v("Goners")]),t._v(")表示。")])]),t._v(" "),s("p",[t._v("在Gone框架中,还包含了三类特殊的Goner,定义如下:")]),t._v(" "),s("h3",{attrs:{id:"🔮prophet-先知"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#🔮prophet-先知"}},[t._v("#")]),t._v(" 🔮Prophet(先知)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",在普通 "),s("strong",[t._v("Goner")]),t._v(" 上实现了 "),s("strong",[s("code",[t._v("AfterRevive() AfterReviveError")])]),t._v(" 方法就是 "),s("strong",[t._v("Prophet(先知)")]),t._v(";"),s("strong",[t._v("AfterRevive")]),t._v(" 会在 "),s("strong",[t._v("Goner")]),t._v(" 被复活后被执行。")]),t._v(" "),s("p",[t._v("Prophet接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Prophet 先知")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Prophet "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//AfterRevive 在Goner复活后会被执行")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" AfterReviveError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"😇angel-天使"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#😇angel-天使"}},[t._v("#")]),t._v(" 😇Angel(天使)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",拥有天使左翼"),s("code",[t._v("Start(Cemetery) error")]),t._v(" 和 天使右翼"),s("code",[t._v("Stop(Cemetery) error")]),t._v(",左翼负责生(用于分配资源,启动某项服务),右翼负责死(用于终止某项服务,回收资源)。")]),t._v(" "),s("p",[t._v("Angel接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Angel "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stop")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"🧛🏻‍♀️vampire-吸血鬼"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#🧛🏻‍♀️vampire-吸血鬼"}},[t._v("#")]),t._v(" 🧛🏻‍♀️Vampire(吸血鬼)")]),t._v(" "),s("p",[t._v("一种特殊的 "),s("strong",[t._v("Goner")]),t._v(",拥有特殊能力——吸血"),s("code",[t._v("Suck(conf string, v reflect.Value) SuckError")]),t._v("。"),s("strong",[t._v("Suck")]),t._v("可以将不是"),s("strong",[t._v("Goner")]),t._v("的值赋予注入给Goner属性。")]),t._v(" "),s("p",[t._v("Vampire接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SuckError "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Vampire "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Suck")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v reflect"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Value"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" SuckError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"cemetery-墓园"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#cemetery-墓园"}},[t._v("#")]),t._v(" Cemetery(墓园)")]),t._v(" "),s("p",[t._v("Cemetery用于管理Goners,主要提供Bury(埋葬)和 revive(复活)的方法,其接口定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cemetery "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// ... 其他方法")]),t._v("\n\tGoner\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Cemetery "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 埋葬,将逝者埋葬到墓园")]),t._v("\n\n\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//ReviveAllFromTombs 复活所有Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReviveAllFromTombs")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("从代码上可以看到Cemetery本身也是一个Goner,在Gone框架启动时他会被自动埋葬和复活。")]),t._v(" "),s("h3",{attrs:{id:"bury-埋葬"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#bury-埋葬"}},[t._v("#")]),t._v(" Bury(埋葬)")]),t._v(" "),s("p",[t._v("将Goner "),s("strong",[t._v("埋葬")]),t._v(" 到 "),s("strong",[t._v("Cemetery")]),t._v(" 就是将Goner注册到框架,以待后续完成属性的注入;在代码实现上,"),s("strong",[t._v("Bury")]),t._v("是"),s("strong",[t._v("Cemetery")]),t._v("上的公开方法,一般在通过 "),s("strong",[t._v("Priest")]),t._v(" 函数调用该方法。")]),t._v(" "),s("h3",{attrs:{id:"revive-复活"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#revive-复活"}},[t._v("#")]),t._v(" Revive(复活)")]),t._v(" "),s("p",[t._v("Revive(复活)指的是Goner所有需要注入的属性完成注入的过程。在函数"),s("code",[t._v("ReviveAllFromTombs() error")]),t._v("中,所有被"),s("strong",[t._v("埋葬")]),t._v("到"),s("strong",[t._v("Cemetery")]),t._v("的Goners都会被尝试复活,如果有属性不能正常注入,程序将panic。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[s("strong",[t._v("ReviveAllFromTombs")]),t._v("在完成了复活所有的"),s("strong",[t._v("Goners")]),t._v("后,会调用所有"),s("strong",[t._v("Prophet")]),t._v("的 "),s("strong",[t._v("AfterRevive")]),t._v("方法。")])]),t._v(" "),s("h2",{attrs:{id:"heaven-天国"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#heaven-天国"}},[t._v("#")]),t._v(" Heaven(天国)")]),t._v(" "),s("p",[t._v("Heaven(天国)代表了一个Gone程序,用于管理程序的启动、停止等状态和流程(复活在启动前完成),用于在启动前后以及程序停止前执行一些hook任务。Heaven接收一个牧师函数开始运行,例如:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用 cemetery.Bury 埋葬 Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者 调用其他 Priest 函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("或者:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 调用 cemetery.Bury 埋葬 Goner")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者 调用其他 Priest 函数")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// TODO")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TODO: 启动后执行一些操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"priest-牧师"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#priest-牧师"}},[t._v("#")]),t._v(" Priest (牧师)")]),t._v(" "),s("p",[t._v("Priest (牧师)是负责将"),s("strong",[t._v("Goner")]),t._v("埋葬到"),s("strong",[t._v("Cemetery")]),t._v("的函数,他的定义如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Priest "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])]),s("p",[t._v("在"),s("strong",[t._v("Priest")]),t._v("函数实现上,可以调用 "),s("strong",[t._v("cemetery.Bury")]),t._v(" 来完成,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss-jim"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker-bob"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名埋葬,不指定被埋葬Goner的GonerId")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"X"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("也可以通过调用其他的"),s("strong",[t._v("Priest")]),t._v("函数来完成:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("aPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a1Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("a2Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("如果我们开发了一个组件包,其中使用了多个"),s("strong",[t._v("Goners")]),t._v("来完成响应的功能,使用时需要同时"),s("strong",[t._v("埋葬")]),t._v("这些"),s("strong",[t._v("Goners")]),t._v(",那么我们可以编写一个"),s("strong",[t._v("Priest")]),t._v("函数方便业务代码批量"),s("strong",[t._v("埋葬")]),t._v("这些"),s("strong",[t._v("Goners")]),t._v("。")]),t._v(" "),s("p",[t._v("框架内置的组件包,我们就是这样干的,"),s("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/goner/priest.go",target:"_blank",rel:"noopener noreferrer"}},[t._v("代码"),s("OutboundLink")],1),t._v(" 和 "),s("a",{attrs:{href:"https://goner.fun/zh/goners/#%E6%A1%86%E6%9E%B6%E5%86%85%E7%BD%AEgoners",target:"_blank",rel:"noopener noreferrer"}},[t._v("文档"),s("OutboundLink")],1),t._v("。")]),t._v(" "),s("p",[t._v("另外,我们开发了一个命令行辅助工具"),s("strong",[t._v("gone")]),t._v(",用于扫描特殊注释"),s("code",[t._v("//go:gone")]),t._v("自动生成牧师函数,参考"),s("RouterLink",{attrs:{to:"/zh/guide/auto-gen-priest.html"}},[t._v("自动生成Priest")]),t._v("。")],1)])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/54.8df9ffd7.js b/assets/js/53.0fb88d1f.js similarity index 99% rename from assets/js/54.8df9ffd7.js rename to assets/js/53.0fb88d1f.js index 4853f75..c8615b1 100644 --- a/assets/js/54.8df9ffd7.js +++ b/assets/js/53.0fb88d1f.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[54],{340:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"错误处理"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#错误处理"}},[t._v("#")]),t._v(" 错误处理")]),t._v(" "),s("h2",{attrs:{id:"gone中定义了三种错误类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone中定义了三种错误类型"}},[t._v("#")]),t._v(" Gone中定义了三种错误类型:")]),t._v(" "),s("ul",[s("li",[s("strong",[t._v("普通错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Error "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Msg")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Code")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ul",[s("li",[s("strong",[t._v("系统内部错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" InnerError "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tError\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stack")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("byte")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ul",[s("li",[s("strong",[t._v("业务错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BusinessError "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tError\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Data")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"为什么要定义这三种错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#为什么要定义这三种错误"}},[t._v("#")]),t._v(" 为什么要定义这三种错误?")]),t._v(" "),s("blockquote",[s("p",[t._v("做人做事,边界很重要,过犹不及;我们希望通过不同类型的错误定义,厘清团队成员职责的边界。写程序,代码要职责清晰;团队成员职责要清晰明确。简单的讲就是要分清“"),s("strong",[t._v("锅")]),t._v("”是谁的,俗话说“是福不是"),s("strong",[t._v("锅")]),t._v(",是"),s("strong",[t._v("锅")]),t._v("躲不过”。")])]),t._v(" "),s("p",[t._v("在Web开发中,我们认为也有三种类型的错误:")]),t._v(" "),s("h3",{attrs:{id:"服务器错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#服务器错误"}},[t._v("#")]),t._v(" 服务器错误")]),t._v(" "),s("p",[t._v("服务器错误对应HTTP状态码的5XX,代表服务器内部错误,比如:数据库连接失败、文件读取失败、网络请求失败等。这种情况下,“"),s("strong",[t._v("锅")]),t._v("”一般是后端同学或者运维同学的,后端同学要尽快定位并解决问题。")]),t._v(" "),s("p",[t._v("在这种情况下,我们可以使用Gone中定义的"),s("code",[t._v("InnerError")]),t._v("来表示系统错误。可以使用函数"),s("code",[t._v("gone.NewInnerError")]),t._v("来创建,这样创建的"),s("code",[t._v("InnerError")]),t._v("最大的好处是携带了堆栈信息,方便我们定位问题。")]),t._v(" "),s("p",[t._v("Gone Web服务器模块的中间件会拦截系统错误,并返回给客户端一个500状态码。")]),t._v(" "),s("h3",{attrs:{id:"客户端错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#客户端错误"}},[t._v("#")]),t._v(" 客户端错误")]),t._v(" "),s("p",[t._v("客户端错误对应HTTP状态码的4XX,代表客户端错误,比如:请求参数错误、请求方法错误、请求路径错误等。这种情况,“"),s("strong",[t._v("锅")]),t._v("”一般是前端同学的,前端同学要尽快定位并解决问题。")]),t._v(" "),s("p",[t._v("前端同学应检查参数是否正确,将正确的请求发送给服务器。服务器也必须做错误检查,如果是客户端问题,则需要返回4XX状态码,错误信息应尽量易读,能够指导前端快速找到问题。这里,我们可以使用"),s("code",[t._v("gone.NewParameterError")]),t._v("来创建Gone中定义的"),s("code",[t._v("Error")]),t._v("来表示客户端错误:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"参数错误"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误信息")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1100")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误码,可以省略,默认为400")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("400")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// HTTP状态码,可以省略,默认为400")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"业务错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#业务错误"}},[t._v("#")]),t._v(" 业务错误")]),t._v(" "),s("p",[t._v("业务错误对应HTTP状态码的200,代表业务错误,比如:用户名或密码错误、用户不存在、用户已存在等。这种情况,“"),s("strong",[t._v("锅")]),t._v("”可能是产品同学的,产品设计问题导致了这种错误。如果错误无法避免,通过产品来引导用户解决;也可能是必要的产品分支设计,比如:用户注册时,需要检查用户名是否已存在。")]),t._v(" "),s("p",[t._v("对于业务错误,可以使用"),s("code",[t._v("gone.NewBusinessError")]),t._v("来创建Gone中定义的"),s("code",[t._v("BusinessError")]),t._v("来表示业务错误,这种错误的最大特点是可以携带业务数据"),s("code",[t._v("Data")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewBusinessError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"订单的信息已经发生变更"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误信息")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2100")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误码,可以省略,默认为0")]),t._v("\n data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 业务数据")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("这三种错误类型的定义能够帮助我们更好地区分和处理不同类型的错误,使代码和团队职责更加清晰明确。")]),t._v(" "),s("h2",{attrs:{id:"总结"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#总结"}},[t._v("#")]),t._v(" 总结")]),t._v(" "),s("p",[t._v("定义三种错误类型的目的是为了更好地区分和处理不同类型的错误,使代码和团队职责更加清晰明确。通过明确划分服务器错误、客户端错误和业务错误,团队成员可以迅速定位并解决问题,确保项目的稳定性和用户体验。系统内部错误(InnerError)帮助后端和运维人员快速定位技术问题;客户端错误(Error)引导前端人员解决请求中的问题;业务错误(BusinessError)则帮助产品和设计人员优化用户流程和体验。这种清晰的职责划分不仅提高了团队的协作效率,也提升了项目的整体质量。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[53],{340:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"错误处理"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#错误处理"}},[t._v("#")]),t._v(" 错误处理")]),t._v(" "),s("h2",{attrs:{id:"gone中定义了三种错误类型"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone中定义了三种错误类型"}},[t._v("#")]),t._v(" Gone中定义了三种错误类型:")]),t._v(" "),s("ul",[s("li",[s("strong",[t._v("普通错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Error "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Msg")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Code")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ul",[s("li",[s("strong",[t._v("系统内部错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" InnerError "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tError\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Stack")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("byte")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ul",[s("li",[s("strong",[t._v("业务错误")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" BusinessError "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tError\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Data")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" any\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"为什么要定义这三种错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#为什么要定义这三种错误"}},[t._v("#")]),t._v(" 为什么要定义这三种错误?")]),t._v(" "),s("blockquote",[s("p",[t._v("做人做事,边界很重要,过犹不及;我们希望通过不同类型的错误定义,厘清团队成员职责的边界。写程序,代码要职责清晰;团队成员职责要清晰明确。简单的讲就是要分清“"),s("strong",[t._v("锅")]),t._v("”是谁的,俗话说“是福不是"),s("strong",[t._v("锅")]),t._v(",是"),s("strong",[t._v("锅")]),t._v("躲不过”。")])]),t._v(" "),s("p",[t._v("在Web开发中,我们认为也有三种类型的错误:")]),t._v(" "),s("h3",{attrs:{id:"服务器错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#服务器错误"}},[t._v("#")]),t._v(" 服务器错误")]),t._v(" "),s("p",[t._v("服务器错误对应HTTP状态码的5XX,代表服务器内部错误,比如:数据库连接失败、文件读取失败、网络请求失败等。这种情况下,“"),s("strong",[t._v("锅")]),t._v("”一般是后端同学或者运维同学的,后端同学要尽快定位并解决问题。")]),t._v(" "),s("p",[t._v("在这种情况下,我们可以使用Gone中定义的"),s("code",[t._v("InnerError")]),t._v("来表示系统错误。可以使用函数"),s("code",[t._v("gone.NewInnerError")]),t._v("来创建,这样创建的"),s("code",[t._v("InnerError")]),t._v("最大的好处是携带了堆栈信息,方便我们定位问题。")]),t._v(" "),s("p",[t._v("Gone Web服务器模块的中间件会拦截系统错误,并返回给客户端一个500状态码。")]),t._v(" "),s("h3",{attrs:{id:"客户端错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#客户端错误"}},[t._v("#")]),t._v(" 客户端错误")]),t._v(" "),s("p",[t._v("客户端错误对应HTTP状态码的4XX,代表客户端错误,比如:请求参数错误、请求方法错误、请求路径错误等。这种情况,“"),s("strong",[t._v("锅")]),t._v("”一般是前端同学的,前端同学要尽快定位并解决问题。")]),t._v(" "),s("p",[t._v("前端同学应检查参数是否正确,将正确的请求发送给服务器。服务器也必须做错误检查,如果是客户端问题,则需要返回4XX状态码,错误信息应尽量易读,能够指导前端快速找到问题。这里,我们可以使用"),s("code",[t._v("gone.NewParameterError")]),t._v("来创建Gone中定义的"),s("code",[t._v("Error")]),t._v("来表示客户端错误:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"参数错误"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误信息")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1100")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误码,可以省略,默认为400")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("400")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// HTTP状态码,可以省略,默认为400")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"业务错误"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#业务错误"}},[t._v("#")]),t._v(" 业务错误")]),t._v(" "),s("p",[t._v("业务错误对应HTTP状态码的200,代表业务错误,比如:用户名或密码错误、用户不存在、用户已存在等。这种情况,“"),s("strong",[t._v("锅")]),t._v("”可能是产品同学的,产品设计问题导致了这种错误。如果错误无法避免,通过产品来引导用户解决;也可能是必要的产品分支设计,比如:用户注册时,需要检查用户名是否已存在。")]),t._v(" "),s("p",[t._v("对于业务错误,可以使用"),s("code",[t._v("gone.NewBusinessError")]),t._v("来创建Gone中定义的"),s("code",[t._v("BusinessError")]),t._v("来表示业务错误,这种错误的最大特点是可以携带业务数据"),s("code",[t._v("Data")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewBusinessError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"订单的信息已经发生变更"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误信息")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2100")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 错误码,可以省略,默认为0")]),t._v("\n data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 业务数据")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("这三种错误类型的定义能够帮助我们更好地区分和处理不同类型的错误,使代码和团队职责更加清晰明确。")]),t._v(" "),s("h2",{attrs:{id:"总结"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#总结"}},[t._v("#")]),t._v(" 总结")]),t._v(" "),s("p",[t._v("定义三种错误类型的目的是为了更好地区分和处理不同类型的错误,使代码和团队职责更加清晰明确。通过明确划分服务器错误、客户端错误和业务错误,团队成员可以迅速定位并解决问题,确保项目的稳定性和用户体验。系统内部错误(InnerError)帮助后端和运维人员快速定位技术问题;客户端错误(Error)引导前端人员解决请求中的问题;业务错误(BusinessError)则帮助产品和设计人员优化用户流程和体验。这种清晰的职责划分不仅提高了团队的协作效率,也提升了项目的整体质量。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file diff --git a/assets/js/54.55e031bc.js b/assets/js/54.55e031bc.js new file mode 100644 index 0000000..3764df4 --- /dev/null +++ b/assets/js/54.55e031bc.js @@ -0,0 +1 @@ +(window.webpackJsonp=window.webpackJsonp||[]).push([[54],{341:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"goner-和-依赖注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner-和-依赖注入"}},[t._v("#")]),t._v(" Goner 和 依赖注入")]),t._v(" "),s("p",[s("strong",[t._v("Gone")]),t._v(" 作为一个依赖注入框架,首先需要回答的问题:什么是依赖,如何定义依赖?")]),t._v(" "),s("p",[t._v("在golang中已经定义了"),s("strong",[t._v("package")]),t._v(",我们在项目中引入的"),s("code",[t._v("package")]),t._v("就是一种依赖;这些依赖可以利用golang项目中的"),s("code",[t._v(".mod")]),t._v("文件进行比较好的管理,然而这种依赖也并不是我们讨论的需要注入的依赖。")]),t._v(" "),s("p",[s("strong",[t._v("“依赖注入”")]),t._v(",作为一个动补短语,要明确它的含义,需要问:什么依赖了什么,谁注入了谁?")]),t._v(" "),s("p",[t._v("在golang中,能够承载业务逻辑的结构,只有 "),s("strong",[s("code",[t._v("func")])]),t._v(" 和 "),s("strong",[s("code",[t._v("struct")])]),t._v(","),s("strong",[s("code",[t._v("func")])]),t._v(" 的执行结果 "),s("strong",[t._v("依赖")]),t._v(" 函数的参数,而 "),s("strong",[s("code",[t._v("struct")])]),t._v(" 功能的实现 "),s("strong",[t._v("依赖")]),t._v(" 结构体的属性。无论是函数的参数 还是 结构体的属性,都是业务逻辑实现的 "),s("strong",[t._v("依赖")]),t._v(";这样我们就回答了“什么是依赖?”:")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("定义")]),t._v(" "),s("p",[s("strong",[t._v("依赖,是为了实现某业务逻辑,函数或者结构体需要依赖的 外部值 或者 外部参数。")])])]),t._v(" "),s("p",[t._v("这些外部值或者外部参数,可能是代表了"),s("strong",[t._v("业务")]),t._v("逻辑依赖的"),s("strong",[t._v("外部业务")]),t._v(";实现我们的业务所需的业务逻辑,需要依赖这些外部业务来完成。由此可见,是 "),s("strong",[t._v("业务")]),t._v(" 依赖了 "),s("strong",[t._v("业务")]),t._v(",那么注入的也是"),s("strong",[t._v("业务")]),t._v(",是 "),s("strong",[t._v("业务")]),t._v(" 注入了 "),s("strong",[t._v("业务")]),t._v(" 。")]),t._v(" "),s("p",[t._v("为了对业务进行抽象,我们定义了"),s("code",[t._v("Goner")]),t._v(",它是一个接口,所有业务结构体都要实现它;那么,在Gone框架中,就是 "),s("strong",[t._v("Goners依赖Goners,Goners注入到Goners")]),t._v("。")]),t._v(" "),s("h2",{attrs:{id:"goner的定义"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner的定义"}},[t._v("#")]),t._v(" Goner的定义")]),t._v(" "),s("p",[t._v("我们查看"),s("a",{attrs:{href:"https://github.com/gone-io/gone/blob/main/interface.go#L13",target:"_blank",rel:"noopener noreferrer"}},[t._v("Goner"),s("OutboundLink")],1),t._v("的定义,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Goner "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("可以看到,Goner是一个接口,要求实现一个私有方法"),s("code",[t._v("goneFlag()")]),t._v(",这个方法是为了标识该结构体是Goner。由于golang语法限制,在包外部是无法实现一个包内的私有方法的,为了在外部的业务代码可以实现 "),s("strong",[t._v("Goner接口")]),t._v(" ,我们又定义了"),s("code",[t._v("gone.Flag")]),t._v("结构体,并且在实现了"),s("code",[t._v("goneFlag()")]),t._v("方法时将方法的接收者设置了为 "),s("strong",[t._v("Flag指针")]),t._v(",如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Flag "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Flag"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("goneFlag")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("这样,业务结构体"),s("code",[t._v("XBusiness")]),t._v(" "),s("strong",[t._v("有且仅有")]),t._v(" 匿名嵌入"),s("code",[t._v("gone.Flag")]),t._v("才能完成对Goner接口的实现。gone框架要求只有Goner才可以被注册到Gone框架中,用于依赖注入的装配。\n例如:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XBusiness "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("这样设计的好处是:")]),t._v(" "),s("ol",[s("li",[t._v("限定依赖注入装配的对象都是Goner,可以简化了依赖注入装配流程实现。")]),t._v(" "),s("li",[t._v("限定Goner一定是指针,依赖注入时可以避免结构体的值拷贝;一方面是提高性能,另一方面是避免值拷贝时的“浅拷贝”业务对象带来的未知问题。")])]),t._v(" "),s("h2",{attrs:{id:"依赖标记"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#依赖标记"}},[t._v("#")]),t._v(" 依赖标记")]),t._v(" "),s("p",[t._v("业务结构体中,并不是所有的属性都需要依赖注入,需要有一种机制标记哪些属性需要使用依赖注入。为此,我们设计了标签"),s("code",[t._v("gone")]),t._v("来标记需要注入的属性,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Employee "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Work")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Company "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tBoss Employee "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 标记需要依赖注入的属性")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("公司需要注入一个员工作为Boss,星号(*)表示该属性的注入只需要匹配类型,即实现 Employee 接口的Goner就可以;这种注入方式,我们称为"),s("strong",[t._v("匿名注入")]),t._v("。")]),t._v(" "),s("div",{staticClass:"custom-block warning"},[s("p",{staticClass:"custom-block-title"},[t._v("需要注意")]),t._v(" "),s("p",[t._v("Goner 结构体需要注入的属性,可以是任意 接口 或者 结构体指针,这里并不限定是Goner,只有Goner注册到Gone框架时限定Goner类型。")])]),t._v(" "),s("p",[t._v("公司的老板不可能是任何员工都能担任的,与"),s("strong",[t._v("匿名注入")]),t._v("对应的就是"),s("strong",[t._v("具名注入")]),t._v(",注入标签可以将星号替换为需要注入Goner的Id 字符串,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Company "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tBoss Employee "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"boss"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 具名注入,要求该属性注入一个Id=boss的Goner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"goners-注册"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goners-注册"}},[t._v("#")]),t._v(" Goners 注册")]),t._v(" "),s("p",[t._v("为了完成依赖注入的自动装配,我们需要将所有的Goners注册到Gone框架中。下面给出上面公司依赖员工这个例子的完整代码,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Employee "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Work")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Name")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Company "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tBoss Employee "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"boss"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 标记需要依赖注入的属性")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Company"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Company start, boss is %s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" c"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Name")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Work")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" EmployeeImpl "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("EmployeeImpl"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Work")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"I am working, my name is %s\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Name")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("EmployeeImpl"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Name")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册EmployeeImpl")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("EmployeeImpl"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"Scott"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GonerId")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册Company")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Company"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("company "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Company"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tcompany"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Start")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("可以看到,我们使用了"),s("code",[t._v("cemetery.Bury")]),t._v(" 方法完成了Goner到Gone框架的注册;该方法第二参数可以指定被注册的Goner的Id,如果没有指定Id,Gone框架会自动为Goner随机生成一个Id。在"),s("strong",[t._v("具名注入")]),t._v("时,我们会用到注册时的GonerId来作为gone标签的值。")]),t._v(" "),s("h2",{attrs:{id:"priest函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#priest函数"}},[t._v("#")]),t._v(" Priest函数")]),t._v(" "),s("p",[t._v("在上面代码中,"),s("code",[t._v("gone.Prepare")]),t._v(" 方法接收的参数为一个匿名函数,它的形式如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])]),s("p",[t._v("在Gone框架中,这样形式的函数被定义为"),s("strong",[t._v("Priest")]),t._v("函数,专门负责Goners的注册。")]),t._v(" "),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("Priest的定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Priest "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n")])])])]),t._v(" "),s("p",[t._v("大多数情况下,我们可以不用手动编写Priest代码;在gone中提供了 "),s("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),s("OutboundLink")],1),t._v(",可以为我们自动生成Priest函数代码:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/auto-gen-priest.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("自动生成Priest\n"),s("OutboundLink")],1)])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/55.6446c903.js b/assets/js/55.687e81f8.js similarity index 99% rename from assets/js/55.6446c903.js rename to assets/js/55.687e81f8.js index deac664..cf05fb6 100644 --- a/assets/js/55.6446c903.js +++ b/assets/js/55.687e81f8.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[55],{343:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"对比-gone-和-spring-的依赖注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#对比-gone-和-spring-的依赖注入"}},[t._v("#")]),t._v(" 对比 Gone 和 Spring 的依赖注入")]),t._v(" "),s("p",[t._v("Gone是我们开发的 Golang 依赖注入框架,Spring是Java的依赖注入框架;为了便于Spring程序员转Golang快速上手Gone,我们在这里对他们的依赖注入做一下对比。")]),t._v(" "),s("h2",{attrs:{id:"依赖模块导入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#依赖模块导入"}},[t._v("#")]),t._v(" 依赖模块导入")]),t._v(" "),s("h3",{attrs:{id:"spring"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在项目管理工具中引入依赖;项目启动后自动扫描加载。\n在 Maven 中 引入依赖")]),t._v(" "),s("div",{staticClass:"language-xml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-xml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("dependency")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("groupId")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("com.github.example"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("artifactId")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("example"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("1.0.0"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n")])])]),s("p",[t._v("在 Gradle 中 引入依赖")]),t._v(" "),s("div",{staticClass:"language-groovy extra-class"},[s("pre",{pre:!0,attrs:{class:"language-groovy"}},[s("code",[t._v("implementation "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'com.github.example:example:1.0.0'")]),t._v("\n")])])]),s("h3",{attrs:{id:"gone"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("p",[t._v("在MasterPriest函数中加载依赖模块提供的Priest函数;在启动前执行"),s("code",[t._v("go mod tidy")]),t._v("将依赖的包写入go.mod文件。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//加载xorm的依赖")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//加载gin的依赖")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("MasterPriest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"注入依赖"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入依赖"}},[t._v("#")]),t._v(" 注入依赖")]),t._v(" "),s("h3",{attrs:{id:"spring-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-2"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v("@Autowired")]),t._v("注解,表示该属性是需要依赖注入的。")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Component")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("DemoController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Autowired")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("IDemo")]),t._v(" demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//该属性是需要依赖注入的")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"goner"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner"}},[t._v("#")]),t._v(" Goner")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v("gone")]),t._v("标签,表示该属性是需要依赖注入的,可以参考"),s("a",{attrs:{href:"https://goner.fun/zh/guide/goner-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone支持哪些方式注入?"),s("OutboundLink")],1)]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//该属性是需要依赖注入的")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"配置注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置注入"}},[t._v("#")]),t._v(" 配置注入")]),t._v(" "),s("h3",{attrs:{id:"spring-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-3"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v('@Value("${key}")')]),t._v("注解,表示该属性是需要依赖注入的。")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Component")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("UseConfig")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Value")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"${my.conf.int}"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Integer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n### "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Gone")]),t._v("\n在结构体属性上添加`gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,${key},default=${defaultValue}"')]),t._v("`标签,表示该属性是需要依赖注入的,可以参考"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("通过内置"),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Goners")]),t._v("支持配置文件"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("https"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("fun"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("zh"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("guide"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("html"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n```go\ntype "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("UseConfig")]),t._v(" struct "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token class-name"}},[s("span",{pre:!0,attrs:{class:"token namespace"}},[t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")])]),t._v("Flag")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int"')]),t._v("`\n\tint8 int8 `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int8"')]),t._v("`\n\tprintInt "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int8"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针 指向int")]),t._v("\n\tfloat64 float64 `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.float64"')]),t._v("`\n\tstring string `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.string"')]),t._v("`\n\tbool bool `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.bool"')]),t._v("`\n\tduration "),s("span",{pre:!0,attrs:{class:"token class-name"}},[s("span",{pre:!0,attrs:{class:"token namespace"}},[t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")])]),t._v("Duration")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.duration"')]),t._v("`\n\tdefaultV string `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.default,default=ok"')]),t._v("`\n\n\tsub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SubConf")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.sub"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针,指向结构体")]),t._v("\n\n\tsubs "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SubConf")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.subs"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//数组")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"http-参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-参数注入"}},[t._v("#")]),t._v(" HTTP 参数注入")]),t._v(" "),s("p",[t._v("HTTP参数注入,就是自动读取HTTP请求的Query参数,并注入到Controller的方法中。")]),t._v(" "),s("p",[s("strong",[t._v("Gone")]),t._v("支持多种HTTP参数注入,可以取到URL、Header、Body、Cookie、Form、Path等参数,\n参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h3",{attrs:{id:"注入query参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入query参数"}},[t._v("#")]),t._v(" 注入Query参数")]),t._v(" "),s("h4",{attrs:{id:"spring-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-4"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@GetMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET方法,当前路由是/demo/hello")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("hello")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestParam")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Query中的name参数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-2"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET 方法")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 当前路由是/demo/hello")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Query中的name参数")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"注入路径参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入路径参数"}},[t._v("#")]),t._v(" 注入路径参数")]),t._v(" "),s("h4",{attrs:{id:"spring-5"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-5"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@GetMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hell/{name}"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置路径模式")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("demo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@PathVariable")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("value "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路径参数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-3"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET 方法")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置路径模式")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路径参数")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求体注入"}},[t._v("#")]),t._v(" 请求体注入")]),t._v(" "),s("h4",{attrs:{id:"spring-6"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-6"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//订阅接收请求的POJO")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("static")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Req")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("GetName")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("void")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SetName")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("this")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@PostMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("demo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestBody")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Req")]),t._v(" req "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入请求体")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-4"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"name"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入请求体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("strong",[t._v("Gone")]),t._v("支持多种HTTP参数注入,可以取到URL、Header、Body、Cookie、Form、Path等参数,\n参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"总结"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#总结"}},[t._v("#")]),t._v(" 总结")]),t._v(" "),s("p",[s("strong",[t._v("Spring")]),t._v("和"),s("strong",[t._v("Gone")]),t._v("在依赖模块导入、依赖注入、配置注入以及HTTP参数注入方面有不同的实现方式。")]),t._v(" "),s("p",[t._v("Spring通过项目管理工具(如Maven或Gradle)引入依赖,并自动扫描加载依赖模块。依赖注入使用"),s("code",[t._v("@Autowired")]),t._v("注解,配置注入使用"),s("code",[t._v('@Value("${key}")')]),t._v("注解,HTTP参数注入则通过"),s("code",[t._v("@RequestParam")]),t._v("、"),s("code",[t._v("@PathVariable")]),t._v("和"),s("code",[t._v("@RequestBody")]),t._v("注解实现。")]),t._v(" "),s("p",[t._v("相对而言,Gone在"),s("code",[t._v("MasterPriest")]),t._v("函数中手动加载依赖模块,通过"),s("code",[t._v("gone")]),t._v("标签进行依赖和配置注入,并支持多种HTTP参数注入方式(如URL、Header、Body、Cookie、Form、Path等),灵活处理HTTP请求参数。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[55],{347:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"对比-gone-和-spring-的依赖注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#对比-gone-和-spring-的依赖注入"}},[t._v("#")]),t._v(" 对比 Gone 和 Spring 的依赖注入")]),t._v(" "),s("p",[t._v("Gone是我们开发的 Golang 依赖注入框架,Spring是Java的依赖注入框架;为了便于Spring程序员转Golang快速上手Gone,我们在这里对他们的依赖注入做一下对比。")]),t._v(" "),s("h2",{attrs:{id:"依赖模块导入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#依赖模块导入"}},[t._v("#")]),t._v(" 依赖模块导入")]),t._v(" "),s("h3",{attrs:{id:"spring"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在项目管理工具中引入依赖;项目启动后自动扫描加载。\n在 Maven 中 引入依赖")]),t._v(" "),s("div",{staticClass:"language-xml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-xml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("dependency")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("groupId")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("com.github.example"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("artifactId")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("example"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("<")]),t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(">")])]),t._v("1.0.0"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token tag"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("")])]),t._v("\n")])])]),s("p",[t._v("在 Gradle 中 引入依赖")]),t._v(" "),s("div",{staticClass:"language-groovy extra-class"},[s("pre",{pre:!0,attrs:{class:"language-groovy"}},[s("code",[t._v("implementation "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("'com.github.example:example:1.0.0'")]),t._v("\n")])])]),s("h3",{attrs:{id:"gone"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("p",[t._v("在MasterPriest函数中加载依赖模块提供的Priest函数;在启动前执行"),s("code",[t._v("go mod tidy")]),t._v("将依赖的包写入go.mod文件。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//加载xorm的依赖")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//加载gin的依赖")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("MasterPriest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"注入依赖"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入依赖"}},[t._v("#")]),t._v(" 注入依赖")]),t._v(" "),s("h3",{attrs:{id:"spring-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-2"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v("@Autowired")]),t._v("注解,表示该属性是需要依赖注入的。")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Component")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("DemoController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Autowired")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("IDemo")]),t._v(" demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//该属性是需要依赖注入的")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"goner"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#goner"}},[t._v("#")]),t._v(" Goner")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v("gone")]),t._v("标签,表示该属性是需要依赖注入的,可以参考"),s("a",{attrs:{href:"https://goner.fun/zh/guide/goner-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone支持哪些方式注入?"),s("OutboundLink")],1)]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//该属性是需要依赖注入的")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"配置注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置注入"}},[t._v("#")]),t._v(" 配置注入")]),t._v(" "),s("h3",{attrs:{id:"spring-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-3"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("p",[t._v("在结构体属性上添加"),s("code",[t._v('@Value("${key}")')]),t._v("注解,表示该属性是需要依赖注入的。")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Component")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("UseConfig")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@Value")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"${my.conf.int}"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Integer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n### "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Gone")]),t._v("\n在结构体属性上添加`gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,${key},default=${defaultValue}"')]),t._v("`标签,表示该属性是需要依赖注入的,可以参考"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("通过内置"),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Goners")]),t._v("支持配置文件"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("https"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("fun"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("zh"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("guide"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("html"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n```go\ntype "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("UseConfig")]),t._v(" struct "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token class-name"}},[s("span",{pre:!0,attrs:{class:"token namespace"}},[t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")])]),t._v("Flag")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int"')]),t._v("`\n\tint8 int8 `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int8"')]),t._v("`\n\tprintInt "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("int")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.int8"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针 指向int")]),t._v("\n\tfloat64 float64 `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.float64"')]),t._v("`\n\tstring string `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.string"')]),t._v("`\n\tbool bool `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.bool"')]),t._v("`\n\tduration "),s("span",{pre:!0,attrs:{class:"token class-name"}},[s("span",{pre:!0,attrs:{class:"token namespace"}},[t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")])]),t._v("Duration")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.duration"')]),t._v("`\n\tdefaultV string `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.default,default=ok"')]),t._v("`\n\n\tsub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SubConf")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.sub"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//指针,指向结构体")]),t._v("\n\n\tsubs "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SubConf")]),t._v(" `gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config,my.conf.subs"')]),t._v("` "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//数组")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"http-参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-参数注入"}},[t._v("#")]),t._v(" HTTP 参数注入")]),t._v(" "),s("p",[t._v("HTTP参数注入,就是自动读取HTTP请求的Query参数,并注入到Controller的方法中。")]),t._v(" "),s("p",[s("strong",[t._v("Gone")]),t._v("支持多种HTTP参数注入,可以取到URL、Header、Body、Cookie、Form、Path等参数,\n参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h3",{attrs:{id:"注入query参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入query参数"}},[t._v("#")]),t._v(" 注入Query参数")]),t._v(" "),s("h4",{attrs:{id:"spring-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-4"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@GetMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET方法,当前路由是/demo/hello")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("hello")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestParam")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Query中的name参数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-2"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET 方法")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 当前路由是/demo/hello")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Query中的name参数")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"注入路径参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入路径参数"}},[t._v("#")]),t._v(" 注入路径参数")]),t._v(" "),s("h4",{attrs:{id:"spring-5"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-5"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@GetMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hell/{name}"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置路径模式")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("demo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@PathVariable")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("value "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路径参数")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-3"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//GET 方法")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置路径模式")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路径参数")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求体注入"}},[t._v("#")]),t._v(" 请求体注入")]),t._v(" "),s("h4",{attrs:{id:"spring-6"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#spring-6"}},[t._v("#")]),t._v(" Spring")]),t._v(" "),s("div",{staticClass:"language-java extra-class"},[s("pre",{pre:!0,attrs:{class:"language-java"}},[s("code",[s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RestController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//路由分组为/demo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("CallbackController")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//订阅接收请求的POJO")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("static")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("class")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Req")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("private")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("GetName")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("void")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("SetName")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("this")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@PostMapping")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("public")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("String")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("demo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token annotation punctuation"}},[t._v("@RequestBody")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token class-name"}},[t._v("Req")]),t._v(" req "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入请求体")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h4",{attrs:{id:"gone-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone-4"}},[t._v("#")]),t._v(" Gone")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\trootRouter gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RouteGroup "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入根路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GinMountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`json:"name"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入请求体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("strong",[t._v("Gone")]),t._v("支持多种HTTP参数注入,可以取到URL、Header、Body、Cookie、Form、Path等参数,\n参考:"),s("a",{attrs:{href:"https://goner.fun/zh/references/http-inject.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("HTTP 注入说明"),s("OutboundLink")],1)]),t._v(" "),s("h2",{attrs:{id:"总结"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#总结"}},[t._v("#")]),t._v(" 总结")]),t._v(" "),s("p",[s("strong",[t._v("Spring")]),t._v("和"),s("strong",[t._v("Gone")]),t._v("在依赖模块导入、依赖注入、配置注入以及HTTP参数注入方面有不同的实现方式。")]),t._v(" "),s("p",[t._v("Spring通过项目管理工具(如Maven或Gradle)引入依赖,并自动扫描加载依赖模块。依赖注入使用"),s("code",[t._v("@Autowired")]),t._v("注解,配置注入使用"),s("code",[t._v('@Value("${key}")')]),t._v("注解,HTTP参数注入则通过"),s("code",[t._v("@RequestParam")]),t._v("、"),s("code",[t._v("@PathVariable")]),t._v("和"),s("code",[t._v("@RequestBody")]),t._v("注解实现。")]),t._v(" "),s("p",[t._v("相对而言,Gone在"),s("code",[t._v("MasterPriest")]),t._v("函数中手动加载依赖模块,通过"),s("code",[t._v("gone")]),t._v("标签进行依赖和配置注入,并支持多种HTTP参数注入方式(如URL、Header、Body、Cookie、Form、Path等),灵活处理HTTP请求参数。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/57.387b9de7.js b/assets/js/57.4d7620df.js similarity index 99% rename from assets/js/57.387b9de7.js rename to assets/js/57.4d7620df.js index 42bf40d..cfee80b 100644 --- a/assets/js/57.387b9de7.js +++ b/assets/js/57.4d7620df.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[57],{344:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"如何优雅使用内置goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#如何优雅使用内置goners"}},[t._v("#")]),t._v(" 如何优雅使用内置Goners?")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#内置goners"}},[t._v("内置Goners")])]),s("li",[s("a",{attrs:{href:"#priest-函数"}},[t._v("Priest函数")])]),s("li",[s("a",{attrs:{href:"#如何使用"}},[t._v("如何使用")])]),s("li",[s("a",{attrs:{href:"#内置goner的goneid"}},[t._v("内置Goner的GoneId")])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"内置goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#内置goners"}},[t._v("#")]),t._v(" 内置Goners")]),t._v(" "),s("p",[t._v("为了降低Gone的使用门槛,我们开发了一些"),s("RouterLink",{attrs:{to:"/zh/goners/#框架内置goners"}},[t._v("内置Goners")]),t._v(",用于提供一些基础的功能。")],1),t._v(" "),s("h2",{attrs:{id:"priest函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#priest函数"}},[t._v("#")]),t._v(" "),s("code",[t._v("Priest")]),t._v("函数")]),t._v(" "),s("p",[t._v("在gone框架内定义了"),s("code",[t._v("Priest")]),t._v("函数,在该函数里可以批量的埋葬用到的Goner;另外"),s("code",[t._v("Priest")]),t._v("函数可以嵌套,即A可以调用B,B可以调用C。那么,在我们开完一组功能后,可以提供一个"),s("code",[t._v("Priest")]),t._v("函数,将用到所有Goner批量埋葬。")]),t._v(" "),s("p",[t._v("实际上,在开发框架内置Goners,我们就采用这样的方式;另外为了方便调用,还在"),s("code",[t._v("goner/priest.go")]),t._v("中定义了一些快捷方式,代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" goner\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/tracer"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/urllib"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" tracer"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" schedule"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UrllibPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" urllib"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"如何使用"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#如何使用"}},[t._v("#")]),t._v(" 如何使用")]),t._v(" "),s("p",[t._v("这样,我们如果要使用基本的日志、trace、配置功能,我们只需要:")]),t._v(" "),s("ol",[s("li",[s("p",[t._v("在主"),s("code",[t._v("Priest")]),t._v("函数中增加"),s("code",[t._v("gone.BasePriest(cemetery)")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 埋葬与日志、trace和配置相关的Goners")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用其他 Priest")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),s("li",[s("p",[t._v("如果需要打印日志,在需要的Goner上注入")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoService")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoService"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoService "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlogrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的匿名属性上")]),t._v("\n\n\tlog logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoService"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用继承的方法")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用属性上的方法")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("注入内置组件,应该使用具名注入")])]),t._v(" "),s("h2",{attrs:{id:"内置goner的goneid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#内置goner的goneid"}},[t._v("#")]),t._v(" 内置Goner的GoneId")]),t._v(" "),s("p",[t._v("内置Goner的GonerId,集中定义在"),s("code",[t._v("https://github.com/gone-io/gone/blob/main/ids.go")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gone\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Gone框架中的内置组件ID")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneHeaven 天堂组件的ID,代码了程序本身,Gone程序启动时默认注入")]),t._v("\n\tIdGoneHeaven "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-heaven"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneCemetery 坟墓组件的ID,是完成依赖注入的关键组件,Gone程序启动时默认注入")]),t._v("\n\tIdGoneCemetery "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cemetery"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneTestKit 测试箱,调用 gone.Test 或者 gone.TestAt 时,会将测试箱注入到程序;非测试代码中不应该注入该组件")]),t._v("\n\tIdGoneTestKit "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-test-kit"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 配置、日志、Tracer 一起构成Gone框架的基础Goner,可以使用 [goner.BasePriest](goner#BasePriest) 牧师函数批量安葬")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdConfig 配置 Goner 的ID,提过能配置能力")]),t._v("\n\tIdConfig "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneConfigure 配置器 Goner 的ID")]),t._v("\n\tIdGoneConfigure "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-configure"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneTracer Tracer Goner 的ID,提供日志追踪能力")]),t._v("\n\tIdGoneTracer "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-tracer"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneLogger 日志 Goner 的ID,用于日志打印")]),t._v("\n\tIdGoneLogger "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-logger"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneCumx [cmux Goner](/goner/cmux#Server) ID")]),t._v("\n\tIdGoneCumx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cumx"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneGin Gin相关的组件ID,可以使用 [goner.GinPriest](goner#GinPriest) 牧师函数批量安葬")]),t._v("\n\tIdGoneGin "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin"')]),t._v("\n\tIdGoneGinRouter "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-router"')]),t._v("\n\tIdGoneGinProcessor "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-processor"')]),t._v("\n\tIdGoneGinProxy "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-proxy"')]),t._v("\n\tIdGoneGinResponser "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-responser"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneXorm Xorm Goner 的ID,封装了xorm,用于操作数据库;使用 [goner.XormPriest](goner#XormPriest) 牧师函数安葬")]),t._v("\n\tIdGoneXorm "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-xorm"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneRedisPool redis pool goner; redis 相关 Goner,使用 [goner.RedisPriest](goner#RedisPriest) 牧师函数安葬")]),t._v("\n\tIdGoneRedisPool "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-pool"')]),t._v("\n\tIdGoneRedisCache "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-cache"')]),t._v("\n\tIdGoneRedisKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-key"')]),t._v("\n\tIdGoneRedisLocker "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-locker"')]),t._v("\n\tIdGoneRedisProvider "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-provider"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneSchedule 定时器Goner;使用 [goner.SchedulePriest](goner#SchedulePriest) 牧师函数安葬")]),t._v("\n\tIdGoneSchedule "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-schedule"')]),t._v("\n\n\tIdGoneReq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-urllib"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[57],{343:function(t,s,n){"use strict";n.r(s);var e=n(10),a=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"如何优雅使用内置goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#如何优雅使用内置goners"}},[t._v("#")]),t._v(" 如何优雅使用内置Goners?")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#内置goners"}},[t._v("内置Goners")])]),s("li",[s("a",{attrs:{href:"#priest-函数"}},[t._v("Priest函数")])]),s("li",[s("a",{attrs:{href:"#如何使用"}},[t._v("如何使用")])]),s("li",[s("a",{attrs:{href:"#内置goner的goneid"}},[t._v("内置Goner的GoneId")])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"内置goners"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#内置goners"}},[t._v("#")]),t._v(" 内置Goners")]),t._v(" "),s("p",[t._v("为了降低Gone的使用门槛,我们开发了一些"),s("RouterLink",{attrs:{to:"/zh/goners/#框架内置goners"}},[t._v("内置Goners")]),t._v(",用于提供一些基础的功能。")],1),t._v(" "),s("h2",{attrs:{id:"priest函数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#priest函数"}},[t._v("#")]),t._v(" "),s("code",[t._v("Priest")]),t._v("函数")]),t._v(" "),s("p",[t._v("在gone框架内定义了"),s("code",[t._v("Priest")]),t._v("函数,在该函数里可以批量的埋葬用到的Goner;另外"),s("code",[t._v("Priest")]),t._v("函数可以嵌套,即A可以调用B,B可以调用C。那么,在我们开完一组功能后,可以提供一个"),s("code",[t._v("Priest")]),t._v("函数,将用到所有Goner批量埋葬。")]),t._v(" "),s("p",[t._v("实际上,在开发框架内置Goners,我们就采用这样的方式;另外为了方便调用,还在"),s("code",[t._v("goner/priest.go")]),t._v("中定义了一些快捷方式,代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" goner\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/tracer"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/urllib"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" tracer"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GinPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" schedule"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UrllibPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" urllib"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"如何使用"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#如何使用"}},[t._v("#")]),t._v(" 如何使用")]),t._v(" "),s("p",[t._v("这样,我们如果要使用基本的日志、trace、配置功能,我们只需要:")]),t._v(" "),s("ol",[s("li",[s("p",[t._v("在主"),s("code",[t._v("Priest")]),t._v("函数中增加"),s("code",[t._v("gone.BasePriest(cemetery)")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 埋葬与日志、trace和配置相关的Goners")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//调用其他 Priest")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),t._v(" "),s("li",[s("p",[t._v("如果需要打印日志,在需要的Goner上注入")])])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" demo\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/logrus"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoService")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoService"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoService "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlogrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的匿名属性上")]),t._v("\n\n\tlog logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoService"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用继承的方法")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用属性上的方法")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("div",{staticClass:"custom-block tip"},[s("p",{staticClass:"custom-block-title"},[t._v("提示")]),t._v(" "),s("p",[t._v("注入内置组件,应该使用具名注入")])]),t._v(" "),s("h2",{attrs:{id:"内置goner的goneid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#内置goner的goneid"}},[t._v("#")]),t._v(" 内置Goner的GoneId")]),t._v(" "),s("p",[t._v("内置Goner的GonerId,集中定义在"),s("code",[t._v("https://github.com/gone-io/gone/blob/main/ids.go")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gone\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Gone框架中的内置组件ID")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneHeaven 天堂组件的ID,代码了程序本身,Gone程序启动时默认注入")]),t._v("\n\tIdGoneHeaven "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-heaven"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneCemetery 坟墓组件的ID,是完成依赖注入的关键组件,Gone程序启动时默认注入")]),t._v("\n\tIdGoneCemetery "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cemetery"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneTestKit 测试箱,调用 gone.Test 或者 gone.TestAt 时,会将测试箱注入到程序;非测试代码中不应该注入该组件")]),t._v("\n\tIdGoneTestKit "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-test-kit"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 配置、日志、Tracer 一起构成Gone框架的基础Goner,可以使用 [goner.BasePriest](goner#BasePriest) 牧师函数批量安葬")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdConfig 配置 Goner 的ID,提过能配置能力")]),t._v("\n\tIdConfig "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneConfigure 配置器 Goner 的ID")]),t._v("\n\tIdGoneConfigure "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-configure"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneTracer Tracer Goner 的ID,提供日志追踪能力")]),t._v("\n\tIdGoneTracer "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-tracer"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneLogger 日志 Goner 的ID,用于日志打印")]),t._v("\n\tIdGoneLogger "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-logger"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneCumx [cmux Goner](/goner/cmux#Server) ID")]),t._v("\n\tIdGoneCumx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-cumx"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneGin Gin相关的组件ID,可以使用 [goner.GinPriest](goner#GinPriest) 牧师函数批量安葬")]),t._v("\n\tIdGoneGin "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin"')]),t._v("\n\tIdGoneGinRouter "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-router"')]),t._v("\n\tIdGoneGinProcessor "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-processor"')]),t._v("\n\tIdGoneGinProxy "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-proxy"')]),t._v("\n\tIdGoneGinResponser "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gin-responser"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneXorm Xorm Goner 的ID,封装了xorm,用于操作数据库;使用 [goner.XormPriest](goner#XormPriest) 牧师函数安葬")]),t._v("\n\tIdGoneXorm "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-xorm"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//IdGoneRedisPool redis pool goner; redis 相关 Goner,使用 [goner.RedisPriest](goner#RedisPriest) 牧师函数安葬")]),t._v("\n\tIdGoneRedisPool "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-pool"')]),t._v("\n\tIdGoneRedisCache "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-cache"')]),t._v("\n\tIdGoneRedisKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-key"')]),t._v("\n\tIdGoneRedisLocker "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-locker"')]),t._v("\n\tIdGoneRedisProvider "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-redis-provider"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// IdGoneSchedule 定时器Goner;使用 [goner.SchedulePriest](goner#SchedulePriest) 牧师函数安葬")]),t._v("\n\tIdGoneSchedule "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-schedule"')]),t._v("\n\n\tIdGoneReq "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-urllib"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/58.7892ea7c.js b/assets/js/58.85495143.js similarity index 99% rename from assets/js/58.7892ea7c.js rename to assets/js/58.85495143.js index 578b07c..4180eb9 100644 --- a/assets/js/58.7892ea7c.js +++ b/assets/js/58.85495143.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[58],{346:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"日志输出"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#日志输出"}},[t._v("#")]),t._v(" 日志输出")]),t._v(" "),s("p",[t._v("在Gone中,提供了一个内置Goner用于日志输出——"),s("a",{attrs:{href:"github.com/gone-io/gone/tree/main/goner/logrus"}},[t._v("logrus")]),t._v(",是通过将"),s("code",[t._v("github.com/sirupsen/logrus")]),t._v("包封装为Goner实现的。希望有小伙伴封装其他的日志输出包,提供更多的日志解决方法。")]),t._v(" "),s("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: 了解Gone的核心概念和术语请阅读:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在这里,我们采用"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner")]),t._v("包中的 "),s("strong",[s("code",[t._v("BasePriest")])]),t._v(" 来完成相关Goners的埋葬。在"),s("code",[t._v("BasePriest")]),t._v("中同时被埋葬到Cemetery还包括配置和trace相关的Goners,这三个包一般一起使用。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬其他Goners")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"在配置文件中添加相关配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#在配置文件中添加相关配置"}},[t._v("#")]),t._v(" 在配置文件中添加相关配置")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: "),s("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),s("OutboundLink")],1)])]),t._v(" "),s("p",[s("strong",[t._v("支持的配置项")])]),t._v(" "),s("ul",[s("li",[s("p",[t._v("log.level,日志级别,默认为info;支持的级别:")]),t._v(" "),s("ul",[s("li",[t._v("panic")]),t._v(" "),s("li",[t._v("fatal")]),t._v(" "),s("li",[t._v("error")]),t._v(" "),s("li",[t._v("warn 或者 warning")]),t._v(" "),s("li",[t._v("info")]),t._v(" "),s("li",[t._v("debug")]),t._v(" "),s("li",[t._v("trace")])])]),t._v(" "),s("li",[s("p",[t._v("log.report-caller,如果为true日志将打印调用日志输出位置的文件名和行号,例如:")]),t._v(" "),s("p",[t._v("2024-05-11 09:09:57.784|INFO|"),s("strong",[t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46")]),t._v("|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080")])]),t._v(" "),s("li",[s("p",[t._v("log.output,日志输出的位置,默认为stdout(标准输出),支持stderr 和 一个文件路径")]),t._v(" "),s("blockquote",[s("p",[s("strong",[t._v("最佳实践:")]),t._v(" 将应用部署在容器中,日志直接输出到标准输出,由收集组件采集日志,比较成熟的方法比如EFK")])])])]),t._v(" "),s("h2",{attrs:{id:"打印日志"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#打印日志"}},[t._v("#")]),t._v(" 打印日志")]),t._v(" "),s("p",[t._v("使用"),s("code",[t._v("Info")]),t._v("方法打印日志:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//打印日志")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("其他级别的日志打印,参考接口代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Logger 日志接口")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Logger "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Tracef")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Trace")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debug")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Info")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Print")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warn")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warning")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panic")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Traceln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infoln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"日志的格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#日志的格式"}},[t._v("#")]),t._v(" 日志的格式")]),t._v(" "),s("p",[s("code",[t._v("${日志输出时间}|${日志级别}|${打印日志的源代码位置}|${TraceId}|${日志内容}")])]),t._v(" "),s("p",[t._v("例如:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),s("h2",{attrs:{id:"关于traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#关于traceid"}},[t._v("#")]),t._v(" 关于TraceId")]),t._v(" "),s("p",[t._v("在web应用中,我们希望有一个统一的编号来标识同一请求产生的日志。这个统一的Id,就是TraceId,如果有这个Id,排查问题时,我们只需要使用这个Id搜索日志,就可以获取请求的所有日志。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[58],{351:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"日志输出"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#日志输出"}},[t._v("#")]),t._v(" 日志输出")]),t._v(" "),s("p",[t._v("在Gone中,提供了一个内置Goner用于日志输出——"),s("a",{attrs:{href:"github.com/gone-io/gone/tree/main/goner/logrus"}},[t._v("logrus")]),t._v(",是通过将"),s("code",[t._v("github.com/sirupsen/logrus")]),t._v("包封装为Goner实现的。希望有小伙伴封装其他的日志输出包,提供更多的日志解决方法。")]),t._v(" "),s("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: 了解Gone的核心概念和术语请阅读:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在这里,我们采用"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner")]),t._v("包中的 "),s("strong",[s("code",[t._v("BasePriest")])]),t._v(" 来完成相关Goners的埋葬。在"),s("code",[t._v("BasePriest")]),t._v("中同时被埋葬到Cemetery还包括配置和trace相关的Goners,这三个包一般一起使用。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬其他Goners")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"在配置文件中添加相关配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#在配置文件中添加相关配置"}},[t._v("#")]),t._v(" 在配置文件中添加相关配置")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: "),s("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),s("OutboundLink")],1)])]),t._v(" "),s("p",[s("strong",[t._v("支持的配置项")])]),t._v(" "),s("ul",[s("li",[s("p",[t._v("log.level,日志级别,默认为info;支持的级别:")]),t._v(" "),s("ul",[s("li",[t._v("panic")]),t._v(" "),s("li",[t._v("fatal")]),t._v(" "),s("li",[t._v("error")]),t._v(" "),s("li",[t._v("warn 或者 warning")]),t._v(" "),s("li",[t._v("info")]),t._v(" "),s("li",[t._v("debug")]),t._v(" "),s("li",[t._v("trace")])])]),t._v(" "),s("li",[s("p",[t._v("log.report-caller,如果为true日志将打印调用日志输出位置的文件名和行号,例如:")]),t._v(" "),s("p",[t._v("2024-05-11 09:09:57.784|INFO|"),s("strong",[t._v("/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46")]),t._v("|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080")])]),t._v(" "),s("li",[s("p",[t._v("log.output,日志输出的位置,默认为stdout(标准输出),支持stderr 和 一个文件路径")]),t._v(" "),s("blockquote",[s("p",[s("strong",[t._v("最佳实践:")]),t._v(" 将应用部署在容器中,日志直接输出到标准输出,由收集组件采集日志,比较成熟的方法比如EFK")])])])]),t._v(" "),s("h2",{attrs:{id:"打印日志"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#打印日志"}},[t._v("#")]),t._v(" 打印日志")]),t._v(" "),s("p",[t._v("使用"),s("code",[t._v("Info")]),t._v("方法打印日志:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//打印日志")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("其他级别的日志打印,参考接口代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Logger 日志接口")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Logger "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Tracef")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("format "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Trace")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debug")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Info")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Print")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warn")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warning")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panic")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Traceln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Debugln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infoln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warnln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Warningln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Errorln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Fatalln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Panicln")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"日志的格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#日志的格式"}},[t._v("#")]),t._v(" 日志的格式")]),t._v(" "),s("p",[s("code",[t._v("${日志输出时间}|${日志级别}|${打印日志的源代码位置}|${TraceId}|${日志内容}")])]),t._v(" "),s("p",[t._v("例如:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),s("h2",{attrs:{id:"关于traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#关于traceid"}},[t._v("#")]),t._v(" 关于TraceId")]),t._v(" "),s("p",[t._v("在web应用中,我们希望有一个统一的编号来标识同一请求产生的日志。这个统一的Id,就是TraceId,如果有这个Id,排查问题时,我们只需要使用这个Id搜索日志,就可以获取请求的所有日志。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file diff --git a/assets/js/59.27b191ce.js b/assets/js/59.1d75901c.js similarity index 99% rename from assets/js/59.27b191ce.js rename to assets/js/59.1d75901c.js index 747e213..0bb2d07 100644 --- a/assets/js/59.27b191ce.js +++ b/assets/js/59.1d75901c.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[59],{348:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"利用redis提供分布式锁和分布式缓存"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#利用redis提供分布式锁和分布式缓存"}},[t._v("#")]),t._v(" 利用redis提供分布式锁和分布式缓存")]),t._v(" "),s("p",[t._v("在本文中,我们将分享在gone中如何使用分布式缓存和分布式锁,其中分布式锁中实现了一种较为自由的处理方式———“智能锁”,对一个处理函数进行上锁,函数执行中会周期性检测锁过期的剩余时间并自动给锁续期,函数执行完后会自动解锁。")]),t._v(" "),s("h2",{attrs:{id:"第一步-将redis相关goner埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第一步-将redis相关goner埋葬到cemetery"}},[t._v("#")]),t._v(" 第一步:将redis相关Goner埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("什么是 Goner?\n什么是 埋葬?\n什么是 Cemetery?\n参考 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在Priest函数中增加"),s("code",[t._v("_ = goner.RedisPriest(cemetery)")]),t._v(",如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"第二步-在配置文件中增加redis相关配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第二步-在配置文件中增加redis相关配置"}},[t._v("#")]),t._v(" 第二步:在配置文件中增加redis相关配置")]),t._v(" "),s("p",[t._v("创建配置文件 "),s("code",[t._v("config/default.properties")]),t._v(",内容如下:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# redis服务地址,格式为 `host:port`")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.server")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("localhost:6379")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# redis服务密码,不配置默认为空")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.password")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("其中,redis服务地址需要改你能访问到的redis服务地址。")])]),t._v(" "),s("p",[t._v("更多配置:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("redis.max-idle")]),t._v(":最大空闲链接数,不配置默认为2")]),t._v(" "),s("li",[s("code",[t._v("redis.max-active")]),t._v(":最大活跃链接数,不配置默认为10")]),t._v(" "),s("li",[s("code",[t._v("redis.db")]),t._v(":使用的db,不配置默认为0")]),t._v(" "),s("li",[s("code",[t._v("redis.cache.prefix")]),t._v(":key前缀,如果设置了,对redis的增删改查都会拼接该前缀,拼接方式"),s("code",[t._v("${prefix}#${key}")]),t._v(";默认为空")])]),t._v(" "),s("blockquote",[s("p",[t._v("关于配置文件,更多参考:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),s("OutboundLink")],1)])]),t._v(" "),s("h2",{attrs:{id:"第三步-使用redis"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第三步-使用redis"}},[t._v("#")]),t._v(" 第三步,使用redis")]),t._v(" "),s("h3",{attrs:{id:"注入接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入接口"}},[t._v("#")]),t._v(" 注入接口")]),t._v(" "),s("p",[t._v("在需要使用的结构体中注入 接口"),s("code",[t._v("redis.redis.Cache")]),t._v("和 "),s("code",[t._v("redis.Locker")]),t._v(",他们的"),s("code",[t._v("GonerId")]),t._v("分别为:"),s("code",[t._v("gone-redis-cache")]),t._v("和"),s("code",[t._v("gone-redis-locker")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用分布是缓存"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布是缓存"}},[t._v("#")]),t._v(" 使用分布是缓存")]),t._v(" "),s("p",[t._v("请看下面代码中的注释:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("接口上的其他方法:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("Remove(key string) (err error)")]),t._v(":用于删除redis某个key,支持通配符")]),t._v(" "),s("li",[s("code",[t._v("Keys(key string) ([]string, error)")]),t._v(":使用前缀或者通配符查询存在哪些key,⚠️该方法慎用")]),t._v(" "),s("li",[s("code",[t._v("Prefix() string")]),t._v(":获取当前缓存配置的key前缀")])]),t._v(" "),s("h3",{attrs:{id:"使用分布时锁"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布时锁"}},[t._v("#")]),t._v(" 使用分布时锁")]),t._v(" "),s("ol",[s("li",[t._v("锁定一段时间")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("这种方式,使用锁需要保证在锁定的时间内能够执行完所有操作,否则由于锁过期可能会导致问题。")])]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("锁定一个操作,操作没结束会自动给锁续期,操作结束自动解锁")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("这种方式比较智能,姑且将其称为“智能锁”吧!\n推荐使用这种方式,可以无脑使用,降低使用的心智负担。")]),t._v(" "),s("h2",{attrs:{id:"上面例子完整代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#上面例子完整代码"}},[t._v("#")]),t._v(" 上面例子完整代码")]),t._v(" "),s("p",[t._v("例子的源代码可以在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-redis",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("找到")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[59],{346:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"利用redis提供分布式锁和分布式缓存"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#利用redis提供分布式锁和分布式缓存"}},[t._v("#")]),t._v(" 利用redis提供分布式锁和分布式缓存")]),t._v(" "),s("p",[t._v("在本文中,我们将分享在gone中如何使用分布式缓存和分布式锁,其中分布式锁中实现了一种较为自由的处理方式———“智能锁”,对一个处理函数进行上锁,函数执行中会周期性检测锁过期的剩余时间并自动给锁续期,函数执行完后会自动解锁。")]),t._v(" "),s("h2",{attrs:{id:"第一步-将redis相关goner埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第一步-将redis相关goner埋葬到cemetery"}},[t._v("#")]),t._v(" 第一步:将redis相关Goner埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("什么是 Goner?\n什么是 埋葬?\n什么是 Cemetery?\n参考 "),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在Priest函数中增加"),s("code",[t._v("_ = goner.RedisPriest(cemetery)")]),t._v(",如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"第二步-在配置文件中增加redis相关配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第二步-在配置文件中增加redis相关配置"}},[t._v("#")]),t._v(" 第二步:在配置文件中增加redis相关配置")]),t._v(" "),s("p",[t._v("创建配置文件 "),s("code",[t._v("config/default.properties")]),t._v(",内容如下:")]),t._v(" "),s("div",{staticClass:"language-properties extra-class"},[s("pre",{pre:!0,attrs:{class:"language-properties"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# redis服务地址,格式为 `host:port`")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.server")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token value attr-value"}},[t._v("localhost:6379")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# redis服务密码,不配置默认为空")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token key attr-name"}},[t._v("redis.password")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("=")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("其中,redis服务地址需要改你能访问到的redis服务地址。")])]),t._v(" "),s("p",[t._v("更多配置:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("redis.max-idle")]),t._v(":最大空闲链接数,不配置默认为2")]),t._v(" "),s("li",[s("code",[t._v("redis.max-active")]),t._v(":最大活跃链接数,不配置默认为10")]),t._v(" "),s("li",[s("code",[t._v("redis.db")]),t._v(":使用的db,不配置默认为0")]),t._v(" "),s("li",[s("code",[t._v("redis.cache.prefix")]),t._v(":key前缀,如果设置了,对redis的增删改查都会拼接该前缀,拼接方式"),s("code",[t._v("${prefix}#${key}")]),t._v(";默认为空")])]),t._v(" "),s("blockquote",[s("p",[t._v("关于配置文件,更多参考:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),s("OutboundLink")],1)])]),t._v(" "),s("h2",{attrs:{id:"第三步-使用redis"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#第三步-使用redis"}},[t._v("#")]),t._v(" 第三步,使用redis")]),t._v(" "),s("h3",{attrs:{id:"注入接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#注入接口"}},[t._v("#")]),t._v(" 注入接口")]),t._v(" "),s("p",[t._v("在需要使用的结构体中注入 接口"),s("code",[t._v("redis.redis.Cache")]),t._v("和 "),s("code",[t._v("redis.Locker")]),t._v(",他们的"),s("code",[t._v("GonerId")]),t._v("分别为:"),s("code",[t._v("gone-redis-cache")]),t._v("和"),s("code",[t._v("gone-redis-locker")]),t._v(":")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用分布是缓存"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布是缓存"}},[t._v("#")]),t._v(" 使用分布是缓存")]),t._v(" "),s("p",[t._v("请看下面代码中的注释:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("接口上的其他方法:")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("Remove(key string) (err error)")]),t._v(":用于删除redis某个key,支持通配符")]),t._v(" "),s("li",[s("code",[t._v("Keys(key string) ([]string, error)")]),t._v(":使用前缀或者通配符查询存在哪些key,⚠️该方法慎用")]),t._v(" "),s("li",[s("code",[t._v("Prefix() string")]),t._v(":获取当前缓存配置的key前缀")])]),t._v(" "),s("h3",{attrs:{id:"使用分布时锁"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布时锁"}},[t._v("#")]),t._v(" 使用分布时锁")]),t._v(" "),s("ol",[s("li",[t._v("锁定一段时间")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("blockquote",[s("p",[t._v("这种方式,使用锁需要保证在锁定的时间内能够执行完所有操作,否则由于锁过期可能会导致问题。")])]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("锁定一个操作,操作没结束会自动给锁续期,操作结束自动解锁")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("这种方式比较智能,姑且将其称为“智能锁”吧!\n推荐使用这种方式,可以无脑使用,降低使用的心智负担。")]),t._v(" "),s("h2",{attrs:{id:"上面例子完整代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#上面例子完整代码"}},[t._v("#")]),t._v(" 上面例子完整代码")]),t._v(" "),s("p",[t._v("例子的源代码可以在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-redis",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("找到")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"time"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockTime")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/60.b6a7f352.js b/assets/js/60.83974e60.js similarity index 99% rename from assets/js/60.b6a7f352.js rename to assets/js/60.83974e60.js index 20b608a..6aede47 100644 --- a/assets/js/60.b6a7f352.js +++ b/assets/js/60.83974e60.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[60],{361:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"用cron表达式配置定时任务"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#用cron表达式配置定时任务"}},[t._v("#")]),t._v(" 用cron表达式配置定时任务")]),t._v(" "),n("p",[t._v("定时任务对于Web项目基本上时标配,可以通过Gone的内置组件来实现定时任务,支持"),n("code",[t._v("cron")]),t._v("表达式。在Web项目中代码一般都是多节点运行,我们使用了redis作为分布式锁来保证任务每次执行只在一个节点上进行,所以需要先准备redis服务,关于redis相关内容请参考:"),n("a",{attrs:{href:"https://goner.fun/zh/guide/redis.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("利用redis提供分布式锁和分布式缓存"),n("OutboundLink")],1),t._v("。另外定时任务还可以和框架“配置注入”的特性结合,将cron表达式放到配置文件中,参考"),n("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),n("OutboundLink")],1),t._v("。")]),t._v(" "),n("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v("\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("h2",{attrs:{id:"编写定时任务执行的job函数"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#编写定时任务执行的job函数"}},[t._v("#")]),t._v(" 编写定时任务执行的Job函数")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"设置定时任务"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#设置定时任务"}},[t._v("#")]),t._v(" 设置定时任务")]),t._v(" "),n("p",[t._v("实现"),n("code",[t._v("Cron(run schedule.RunFuncOnceAt)")]),t._v(",框架会扫描结构体上的该方法并自动执行,在该方法中设置定时任务。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"*/5 * * * * *"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// cron 表达式,表示每5秒执行一次")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("完整的demo代码如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1 execute"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"*/5 * * * * *"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// cron 表达式,表示每5秒执行一次")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("上面代码会每隔5s打印:"),n("code",[t._v("job1 execute")]),t._v(",是不是很简单?")]),t._v(" "),n("h2",{attrs:{id:"将定时配置放到配置文件中"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#将定时配置放到配置文件中"}},[t._v("#")]),t._v(" 将定时配置放到配置文件中")]),t._v(" "),n("p",[t._v("将定时配置放到配置文件中,代码上只需要做如下3点修改:")]),t._v(" "),n("ol",[n("li",[t._v("将配置文件支持的相关Goner 埋葬到 Cemetery 中")]),t._v(" "),n("li",[t._v("注入放到配置文件的定时任务配置")]),t._v(" "),n("li",[t._v("使用从配置文件注入的定时配置设置定时任务")])]),t._v(" "),n("p",[t._v("修改后的代码如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//1.将配置文件支持的相关Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcron "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,cron.job1,default=*/5 * * * * *"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//2. 注入放到配置文件的定时任务配置")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1 execute"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cron"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 3. 使用从配置文件注入的定时配置")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[60],{348:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"用cron表达式配置定时任务"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#用cron表达式配置定时任务"}},[t._v("#")]),t._v(" 用cron表达式配置定时任务")]),t._v(" "),n("p",[t._v("定时任务对于Web项目基本上时标配,可以通过Gone的内置组件来实现定时任务,支持"),n("code",[t._v("cron")]),t._v("表达式。在Web项目中代码一般都是多节点运行,我们使用了redis作为分布式锁来保证任务每次执行只在一个节点上进行,所以需要先准备redis服务,关于redis相关内容请参考:"),n("a",{attrs:{href:"https://goner.fun/zh/guide/redis.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("利用redis提供分布式锁和分布式缓存"),n("OutboundLink")],1),t._v("。另外定时任务还可以和框架“配置注入”的特性结合,将cron表达式放到配置文件中,参考"),n("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),n("OutboundLink")],1),t._v("。")]),t._v(" "),n("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[t._v("\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),n("h2",{attrs:{id:"编写定时任务执行的job函数"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#编写定时任务执行的job函数"}},[t._v("#")]),t._v(" 编写定时任务执行的Job函数")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"设置定时任务"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#设置定时任务"}},[t._v("#")]),t._v(" 设置定时任务")]),t._v(" "),n("p",[t._v("实现"),n("code",[t._v("Cron(run schedule.RunFuncOnceAt)")]),t._v(",框架会扫描结构体上的该方法并自动执行,在该方法中设置定时任务。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"*/5 * * * * *"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// cron 表达式,表示每5秒执行一次")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("完整的demo代码如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1 execute"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"*/5 * * * * *"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// cron 表达式,表示每5秒执行一次")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("上面代码会每隔5s打印:"),n("code",[t._v("job1 execute")]),t._v(",是不是很简单?")]),t._v(" "),n("h2",{attrs:{id:"将定时配置放到配置文件中"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#将定时配置放到配置文件中"}},[t._v("#")]),t._v(" 将定时配置放到配置文件中")]),t._v(" "),n("p",[t._v("将定时配置放到配置文件中,代码上只需要做如下3点修改:")]),t._v(" "),n("ol",[n("li",[t._v("将配置文件支持的相关Goner 埋葬到 Cemetery 中")]),t._v(" "),n("li",[t._v("注入放到配置文件的定时任务配置")]),t._v(" "),n("li",[t._v("使用从配置文件注入的定时配置设置定时任务")])]),t._v(" "),n("p",[t._v("修改后的代码如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/schedule"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.SchedulePriest 函数,将 定时任务 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("SchedulePriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//1.将配置文件支持的相关Goner 埋葬到 Cemetery 中")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" sch "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcron "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,cron.job1,default=*/5 * * * * *"`')]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//2. 注入放到配置文件的定时任务配置")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("job1")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo 定时任务逻辑")]),t._v("\n\tfmt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1 execute"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("sch "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("sch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Cron")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("run schedule"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("RunFuncOnceAt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 run `RunFuncOnceAt`设置定时任务,")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("run")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cron"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 3. 使用从配置文件注入的定时配置")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"job1"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要设置一个唯一标识,用于 分布式锁加锁")]),t._v("\n\t\tsch"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("job1"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 定时任务逻辑")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Serve")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/61.2268674a.js b/assets/js/61.050d174a.js similarity index 99% rename from assets/js/61.2268674a.js rename to assets/js/61.050d174a.js index 239d951..b158cef 100644 --- a/assets/js/61.2268674a.js +++ b/assets/js/61.050d174a.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[61],{349:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"使用traceid追踪日志"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用traceid追踪日志"}},[t._v("#")]),t._v(" 使用traceId追踪日志")]),t._v(" "),s("p",[t._v("在Web应用中,一次请求可能会经过很多业务流程的处理;为了方便排查问题,我们希望所有业务流程打印的日志拥有一个统一的traceId;拥有traceId,就可以将整个业务流程的日志都串起来,回溯和分析问题出在哪个业务环节。\n在其他开源框架中,一般采用策略是在"),s("strong",[t._v("所有")]),t._v("的函数中都增加"),s("code",[t._v("context.Context")]),t._v("参数,golang官方也是这样推荐的。然而我们认为这样平白无故的多打一些代码实在很难受,我们希望不用在每个函数中传递额外参数实现打印日志中附件traceId。为此,我们在Gone中提供了一个内置的Goner来提供这样的功能。")]),t._v(" "),s("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: 了解Gone的核心概念和术语请阅读:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在这里,我们采用"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner")]),t._v("包中的 "),s("strong",[s("code",[t._v("BasePriest")])]),t._v(" 来完成相关Goners的埋葬。在"),s("code",[t._v("BasePriest")]),t._v("中会将 "),s("code",[t._v("tracer")]),t._v("、"),s("code",[t._v("config")]),t._v("、"),s("code",[t._v("logrus")]),t._v(" 相关的Goners同埋葬到Cemetery,这三个包一般一起使用。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬其他Goners")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"简单使用"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#简单使用"}},[t._v("#")]),t._v(" 简单使用")]),t._v(" "),s("p",[t._v("在埋葬了tracer的情况下,我们通过注入的"),s("code",[t._v("logrus.Logger")]),t._v("接口打印日志时,日志中会自动添加一串traceId。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//打印日志")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("如下,其中"),s("code",[t._v("061ad00f-8c0d-479c-bc4c-393e0cf2cca2")]),t._v("为traceId:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),s("h2",{attrs:{id:"跨协程-goroutine-传递traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#跨协程-goroutine-传递traceid"}},[t._v("#")]),t._v(" 跨协程(goroutine)传递traceId")]),t._v(" "),s("p",[t._v("前面例子中,如果没有新开协程是正常的,如果使用关键词"),s("code",[t._v("go")]),t._v("开一个新的协程,会发现协程打印的日志没有traceId。\n怎么解决,注入"),s("code",[t._v("tracer.Tracer")]),t._v("接口,使用之前的"),s("code",[t._v("Go")]),t._v("方法来代替"),s("code",[t._v("go")]),t._v("关键词来新开协程,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n tracer"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Tracer "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-tracer"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入tracer")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Go")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//新协程中打印日志")]),t._v("\n\t\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"log in new goroutine"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("h2",{attrs:{id:"跨进程-服务传递traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#跨进程-服务传递traceid"}},[t._v("#")]),t._v(" 跨进程/服务传递traceId")]),t._v(" "),s("p",[t._v("在微服务中,一个web请求一般会跨域多个微服务,跨域微服务的方式一般会通过:")]),t._v(" "),s("ol",[s("li",[t._v("消息中间件\n为实现traceId在消息中间件中传递和更方便的使用消息中间件传递业务事件,我们开源了"),s("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://github.com/gone-io/emitter"),s("OutboundLink")],1),t._v("仓库,在这个仓库中实现了"),s("strong",[t._v("Rocket MQ")]),t._v("的适配,后续计划适配"),s("strong",[t._v("Kafka")]),t._v("、"),s("strong",[t._v("RabbitMQ")]),t._v("等主流的消息中间件。")]),t._v(" "),s("li",[t._v("RPC调用/内部http调用\n通过Gone内置的Goner "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/urllib",target:"_blank",rel:"noopener noreferrer"}},[t._v("urllib"),s("OutboundLink")],1),t._v(" 来给Gone Web程序发送http请求,traceId会自动在服务间传递。另外使用内置的 "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/grpc",target:"_blank",rel:"noopener noreferrer"}},[t._v("grpc"),s("OutboundLink")],1),t._v(" 来实现gRpc调用也能自动传递traceId。后续会支持更多的rpc调用。")])]),t._v(" "),s("h2",{attrs:{id:"多语言支持"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#多语言支持"}},[t._v("#")]),t._v(" 多语言支持")]),t._v(" "),s("p",[t._v("在http请求中传递traceId,是通过在请求头中增加了一个特殊的头"),s("code",[t._v("X-Trace-ID")]),t._v("来携带traceId。所以如果使用了多种开发语言,不同服务间只要遵守“在Client端请求时附件"),s("code",[t._v("X-Trace-ID")]),t._v(",在Server端处理请求时解析"),s("code",[t._v("X-Trace-ID")]),t._v("”,就能实现让traceId跨域不同语言开发的微服务。"),s("br"),t._v("\n后续会开发其他语言的包支持其他语言无缝接入。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[61],{354:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"使用traceid追踪日志"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用traceid追踪日志"}},[t._v("#")]),t._v(" 使用traceId追踪日志")]),t._v(" "),s("p",[t._v("在Web应用中,一次请求可能会经过很多业务流程的处理;为了方便排查问题,我们希望所有业务流程打印的日志拥有一个统一的traceId;拥有traceId,就可以将整个业务流程的日志都串起来,回溯和分析问题出在哪个业务环节。\n在其他开源框架中,一般采用策略是在"),s("strong",[t._v("所有")]),t._v("的函数中都增加"),s("code",[t._v("context.Context")]),t._v("参数,golang官方也是这样推荐的。然而我们认为这样平白无故的多打一些代码实在很难受,我们希望不用在每个函数中传递额外参数实现打印日志中附件traceId。为此,我们在Gone中提供了一个内置的Goner来提供这样的功能。")]),t._v(" "),s("h2",{attrs:{id:"将相关goners埋葬到cemetery"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#将相关goners埋葬到cemetery"}},[t._v("#")]),t._v(" 将相关Goners埋葬到Cemetery")]),t._v(" "),s("blockquote",[s("p",[t._v("tip: 了解Gone的核心概念和术语请阅读:"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("Gone的核心概念"),s("OutboundLink")],1)])]),t._v(" "),s("p",[t._v("在这里,我们采用"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner")]),t._v("包中的 "),s("strong",[s("code",[t._v("BasePriest")])]),t._v(" 来完成相关Goners的埋葬。在"),s("code",[t._v("BasePriest")]),t._v("中会将 "),s("code",[t._v("tracer")]),t._v("、"),s("code",[t._v("config")]),t._v("、"),s("code",[t._v("logrus")]),t._v(" 相关的Goners同埋葬到Cemetery,这三个包一般一起使用。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MasterPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("BasePriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬其他Goners")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"简单使用"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#简单使用"}},[t._v("#")]),t._v(" 简单使用")]),t._v(" "),s("p",[t._v("在埋葬了tracer的情况下,我们通过注入的"),s("code",[t._v("logrus.Logger")]),t._v("接口打印日志时,日志中会自动添加一串traceId。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//打印日志")]),t._v("\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"input content is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("p",[t._v("如下,其中"),s("code",[t._v("061ad00f-8c0d-479c-bc4c-393e0cf2cca2")]),t._v("为traceId:")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v("2024-05-11 09:09:57.784|INFO|**/Users/jim/go/pkg/mod/github.com/gone-io/gone@v0.1.4/goner/gin/server.go:46**|061ad00f-8c0d-479c-bc4c-393e0cf2cca2|Server Listen At :8080\n")])])]),s("h2",{attrs:{id:"跨协程-goroutine-传递traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#跨协程-goroutine-传递traceid"}},[t._v("#")]),t._v(" 跨协程(goroutine)传递traceId")]),t._v(" "),s("p",[t._v("前面例子中,如果没有新开协程是正常的,如果使用关键词"),s("code",[t._v("go")]),t._v("开一个新的协程,会发现协程打印的日志没有traceId。\n怎么解决,注入"),s("code",[t._v("tracer.Tracer")]),t._v("接口,使用之前的"),s("code",[t._v("Go")]),t._v("方法来代替"),s("code",[t._v("go")]),t._v("关键词来新开协程,如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" service "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n log logrus"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Logger "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-logger"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名注入到 嵌套的log属性上")]),t._v("\n tracer"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Tracer "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-tracer"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入tracer")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("svc "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Business")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Go")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//新协程中打印日志")]),t._v("\n\t\tsvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("log"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Infof")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"log in new goroutine"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" input"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//...")]),t._v("\n")])])]),s("h2",{attrs:{id:"跨进程-服务传递traceid"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#跨进程-服务传递traceid"}},[t._v("#")]),t._v(" 跨进程/服务传递traceId")]),t._v(" "),s("p",[t._v("在微服务中,一个web请求一般会跨域多个微服务,跨域微服务的方式一般会通过:")]),t._v(" "),s("ol",[s("li",[t._v("消息中间件\n为实现traceId在消息中间件中传递和更方便的使用消息中间件传递业务事件,我们开源了"),s("a",{attrs:{href:"https://github.com/gone-io/emitter",target:"_blank",rel:"noopener noreferrer"}},[t._v("https://github.com/gone-io/emitter"),s("OutboundLink")],1),t._v("仓库,在这个仓库中实现了"),s("strong",[t._v("Rocket MQ")]),t._v("的适配,后续计划适配"),s("strong",[t._v("Kafka")]),t._v("、"),s("strong",[t._v("RabbitMQ")]),t._v("等主流的消息中间件。")]),t._v(" "),s("li",[t._v("RPC调用/内部http调用\n通过Gone内置的Goner "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/urllib",target:"_blank",rel:"noopener noreferrer"}},[t._v("urllib"),s("OutboundLink")],1),t._v(" 来给Gone Web程序发送http请求,traceId会自动在服务间传递。另外使用内置的 "),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/goner/grpc",target:"_blank",rel:"noopener noreferrer"}},[t._v("grpc"),s("OutboundLink")],1),t._v(" 来实现gRpc调用也能自动传递traceId。后续会支持更多的rpc调用。")])]),t._v(" "),s("h2",{attrs:{id:"多语言支持"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#多语言支持"}},[t._v("#")]),t._v(" 多语言支持")]),t._v(" "),s("p",[t._v("在http请求中传递traceId,是通过在请求头中增加了一个特殊的头"),s("code",[t._v("X-Trace-ID")]),t._v("来携带traceId。所以如果使用了多种开发语言,不同服务间只要遵守“在Client端请求时附件"),s("code",[t._v("X-Trace-ID")]),t._v(",在Server端处理请求时解析"),s("code",[t._v("X-Trace-ID")]),t._v("”,就能实现让traceId跨域不同语言开发的微服务。"),s("br"),t._v("\n后续会开发其他语言的包支持其他语言无缝接入。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/62.0e97ae7d.js b/assets/js/62.d0d8036b.js similarity index 99% rename from assets/js/62.0e97ae7d.js rename to assets/js/62.d0d8036b.js index 9baedb1..e1e593c 100644 --- a/assets/js/62.0e97ae7d.js +++ b/assets/js/62.d0d8036b.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[62],{350:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"单元测试"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#单元测试"}},[t._v("#")]),t._v(" 单元测试")]),t._v(" "),s("p",[t._v("在一个依赖注入框架中如何进行单元测试,将依赖项先注入后再测试;如果测试内容和注入内容强相关,可以考虑对相关内容做mock;一种方式是手动将mock后的Goner埋葬到系统,另外一种方式时将已经埋葬的Goner,使用"),s("code",[t._v("cemetery.ReplaceBury")]),t._v("做替换性埋葬。")]),t._v(" "),s("h2",{attrs:{id:"假设我们编写的goner如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#假设我们编写的goner如下"}},[t._v("#")]),t._v(" 假设我们编写的Goner如下")]),t._v(" "),s("p",[t._v("文件名:goner.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameA "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-a"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameB "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-b"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameB\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Point "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tX "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-x"`')]),t._v("\n\tY "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-y,default=200"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Line "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tA "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n\tB "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-b"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"我们可以编写测试文件如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#我们可以编写测试文件如下"}},[t._v("#")]),t._v(" 我们可以编写测试文件如下:")]),t._v(" "),s("p",[t._v("文件名:goner_test.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_Line")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"ReplaceBury"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tMock "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReplaceBury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用gomock做mock测试"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用gomock做mock测试"}},[t._v("#")]),t._v(" 使用gomock做mock测试")]),t._v(" "),s("p",[t._v("为了解耦,我们推荐使用接口进行注入;实际上推荐接口注入的另一个原因是,go语言提供了基于接口的mock方案,我们可以将依赖的内容都mock起来。然而使用"),s("code",[t._v("mockgen")]),t._v("生成的mock实现,不是"),s("code",[t._v("Goner")]),t._v(",无法被埋葬,所以也无法注入;为此,我们在辅助工具中提供了解决方案。")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone mock "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n")])])]),s("p",[s("code",[t._v("gone mock")]),t._v(" 命令可以在一个"),s("code",[t._v("mockgen")]),t._v("生成的mock对象中增加"),s("code",[t._v("gone.Flag")]),t._v(",让其变成一个Goner,可以进入Cemetery。")]),t._v(" "),s("p",[t._v("一般的使用方法是,在需要mock的接口上加"),s("code",[t._v("//go:generate")]),t._v("注释,让生成过程在"),s("code",[t._v("go generate ./...")]),t._v("命令时自动完成,下面是一个例子:")]),t._v(" "),s("p",[t._v("文件名:i_point.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE|gone mock -o mock/$GOFILE"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IPoint "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("上面"),s("code",[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE|gone mock -o mock/$GOFILE"')]),t._v("的作用是,先用mock为该接口生成一个mock对象,然后通过管道传递给"),s("code",[t._v("gone mock")]),t._v("添加"),s("code",[t._v("gone.Flag")]),t._v("标记。")]),t._v(" "),s("blockquote",[s("p",[t._v("注意mockgen工具和gomock包的版本需要保持一致;\n运行下面代码,安装最新版本:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go get github.com/golang/mock/gomock\ngo get github.com/golang/mock/mockgen\n")])])]),s("p",[t._v("需要安装gone辅助工具;安装参考 "),s("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),s("OutboundLink")],1),t._v("。")])]),t._v(" "),s("p",[t._v("好让,我们来试试吧,创建一个空目录并在进入后,将上文件"),s("code",[t._v("i_test.go")]),t._v("创建出来,让后在当前目录运行命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\n")])])]),s("p",[t._v("可以看到,命令运行完后,将生成文件"),s("code",[t._v("mock/i_point.go")]),t._v("。")]),t._v(" "),s("p",[t._v("下面我们创建一个 "),s("code",[t._v("origin_point.go")]),t._v("文件,内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" originPoint "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewOriginPoint")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在创建一个名为"),s("code",[t._v("distance_calculator.go")]),t._v("的文件,内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"math"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" distanceCalculator "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\toriginPoint IPoint "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" y "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\toriginX"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" originY "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sqrt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originX"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("y"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originY"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("code",[t._v("distanceCalculator")]),t._v(" 的业务是计算"),s("code",[t._v("(x,y int)")]),t._v("到originPoint点的距离,"),s("code",[t._v("originPoint")]),t._v("是依赖注入的;现在我们来编写"),s("code",[t._v("CalculateDistanceFromOrigin")]),t._v("的测试函数如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example/test/mock"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/golang/mock/gomock"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_distanceCalculator_CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//创建mock控制器")]),t._v("\n\tcontroller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gomock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Finish")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tdistance "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" distance"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//创建mock对象")]),t._v("\n\t\tpoint "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" mock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewMockIPoint")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//将mock对象埋葬到Cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被测试的对象也需要埋葬到Cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[62],{349:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"单元测试"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#单元测试"}},[t._v("#")]),t._v(" 单元测试")]),t._v(" "),s("p",[t._v("在一个依赖注入框架中如何进行单元测试,将依赖项先注入后再测试;如果测试内容和注入内容强相关,可以考虑对相关内容做mock;一种方式是手动将mock后的Goner埋葬到系统,另外一种方式时将已经埋葬的Goner,使用"),s("code",[t._v("cemetery.ReplaceBury")]),t._v("做替换性埋葬。")]),t._v(" "),s("h2",{attrs:{id:"假设我们编写的goner如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#假设我们编写的goner如下"}},[t._v("#")]),t._v(" 假设我们编写的Goner如下")]),t._v(" "),s("p",[t._v("文件名:goner.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameA "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-a"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" pointNameB "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example-test-point-b"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameB\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Point "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tX "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-x"`')]),t._v("\n\tY "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,example.test.point.a-y,default=200"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Line "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tA "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-a"`')]),t._v("\n\tB "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"example-test-point-b"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Say")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('""')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointA")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPointB")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewLine")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"我们可以编写测试文件如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#我们可以编写测试文件如下"}},[t._v("#")]),t._v(" 我们可以编写测试文件如下:")]),t._v(" "),s("p",[t._v("文件名:goner_test.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_Line")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestAt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pointNameA"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1000")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" config"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"config default"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Y"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"ReplaceBury"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("line "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" line"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("A"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tMock "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("X"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("20")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ReplaceBury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" pointNameA"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用gomock做mock测试"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用gomock做mock测试"}},[t._v("#")]),t._v(" 使用gomock做mock测试")]),t._v(" "),s("p",[t._v("为了解耦,我们推荐使用接口进行注入;实际上推荐接口注入的另一个原因是,go语言提供了基于接口的mock方案,我们可以将依赖的内容都mock起来。然而使用"),s("code",[t._v("mockgen")]),t._v("生成的mock实现,不是"),s("code",[t._v("Goner")]),t._v(",无法被埋葬,所以也无法注入;为此,我们在辅助工具中提供了解决方案。")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone mock "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n")])])]),s("p",[s("code",[t._v("gone mock")]),t._v(" 命令可以在一个"),s("code",[t._v("mockgen")]),t._v("生成的mock对象中增加"),s("code",[t._v("gone.Flag")]),t._v(",让其变成一个Goner,可以进入Cemetery。")]),t._v(" "),s("p",[t._v("一般的使用方法是,在需要mock的接口上加"),s("code",[t._v("//go:generate")]),t._v("注释,让生成过程在"),s("code",[t._v("go generate ./...")]),t._v("命令时自动完成,下面是一个例子:")]),t._v(" "),s("p",[t._v("文件名:i_point.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE|gone mock -o mock/$GOFILE"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IPoint "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("上面"),s("code",[t._v('//go:generate sh -c "mockgen -package=mock -source=$GOFILE|gone mock -o mock/$GOFILE"')]),t._v("的作用是,先用mock为该接口生成一个mock对象,然后通过管道传递给"),s("code",[t._v("gone mock")]),t._v("添加"),s("code",[t._v("gone.Flag")]),t._v("标记。")]),t._v(" "),s("blockquote",[s("p",[t._v("注意mockgen工具和gomock包的版本需要保持一致;\n运行下面代码,安装最新版本:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go get github.com/golang/mock/gomock\ngo get github.com/golang/mock/mockgen\n")])])]),s("p",[t._v("需要安装gone辅助工具;安装参考 "),s("a",{attrs:{href:"https://goner.fun/zh/references/gone-tool.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("gone辅助工具"),s("OutboundLink")],1),t._v("。")])]),t._v(" "),s("p",[t._v("好让,我们来试试吧,创建一个空目录并在进入后,将上文件"),s("code",[t._v("i_test.go")]),t._v("创建出来,让后在当前目录运行命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go generate ./"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v(".\n")])])]),s("p",[t._v("可以看到,命令运行完后,将生成文件"),s("code",[t._v("mock/i_point.go")]),t._v("。")]),t._v(" "),s("p",[t._v("下面我们创建一个 "),s("code",[t._v("origin_point.go")]),t._v("文件,内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" originPoint "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewOriginPoint")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("o "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("200")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在创建一个名为"),s("code",[t._v("distance_calculator.go")]),t._v("的文件,内容如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"math"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" distanceCalculator "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\toriginPoint IPoint "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" y "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("float64")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\toriginX"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" originY "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("originPoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sqrt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("x"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originX"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" math"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Pow")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("y"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("originY"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[s("code",[t._v("distanceCalculator")]),t._v(" 的业务是计算"),s("code",[t._v("(x,y int)")]),t._v("到originPoint点的距离,"),s("code",[t._v("originPoint")]),t._v("是依赖注入的;现在我们来编写"),s("code",[t._v("CalculateDistanceFromOrigin")]),t._v("的测试函数如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" test\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"example/test/mock"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/golang/mock/gomock"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test_distanceCalculator_CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//创建mock控制器")]),t._v("\n\tcontroller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" gomock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Finish")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("distanceCalculator"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tdistance "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("CalculateDistanceFromOrigin")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("3")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("4")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\tassert"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Equal")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("float64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" distance"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//创建mock对象")]),t._v("\n\t\tpoint "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" mock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewMockIPoint")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("controller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetX")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\tpoint"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("EXPECT")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GetY")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Return")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//将mock对象埋葬到Cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("point"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//被测试的对象也需要埋葬到Cemetery")]),t._v("\n\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDistanceCalculator")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/63.5a67024f.js b/assets/js/63.1625e770.js similarity index 99% rename from assets/js/63.5a67024f.js rename to assets/js/63.1625e770.js index 34f73f4..bc996bb 100644 --- a/assets/js/63.5a67024f.js +++ b/assets/js/63.1625e770.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[63],{351:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"通过内置goners支持数据库操作"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持数据库操作"}},[t._v("#")]),t._v(" 通过内置Goners支持数据库操作")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#以mysql举个例子-源代码在-这里-https-github-com-gone-io-gone-tree-main-example-use-rdb-可以找到"}},[t._v("以Mysql举个例子,源代码在这里可以找到")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[t._v("1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务")])]),s("li",[s("a",{attrs:{href:"#_2-创建mod和配置"}},[t._v("2. 创建mod和配置")])]),s("li",[s("a",{attrs:{href:"#_3-编写代码如下"}},[t._v("3. 编写代码如下:")])]),s("li",[s("a",{attrs:{href:"#_3-运行代码"}},[t._v("3. 运行代码")])])])]),s("li",[s("a",{attrs:{href:"#需要注意的点"}},[t._v("需要注意的点")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[t._v("1. github.com/gone-io/gone/goner 是对 xorm.io/xorm 的封装")])]),s("li",[s("a",{attrs:{href:"#_2-通过-gone-xorm-gonerid-注入"}},[t._v("2. 通过gone-xormGonerId注入")])]),s("li",[s("a",{attrs:{href:"#_3-注意导入的包"}},[t._v("3. 注意导入的包")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"以mysql举个例子-源代码在这里可以找到"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#以mysql举个例子-源代码在这里可以找到"}},[t._v("#")]),t._v(" 以Mysql举个例子,源代码在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-rdb",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("可以找到")]),t._v(" "),s("h3",{attrs:{id:"_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[t._v("#")]),t._v(" 1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务")]),t._v(" "),s("p",[t._v("docker-compose.yaml 内容如下:")]),t._v(" "),s("div",{staticClass:"language-yaml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-yaml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3.5"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("services")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("mysql")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("image")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8.3")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("restart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" unless"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v("stopped\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("environment")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_ROOT_PASSWORD")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("123456")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_DATABASE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" demo\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("volumes")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" ./data/mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("/var/lib/mysql\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("ports")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3306:3306"')]),t._v("\n")])])]),s("p",[t._v("命令行执行下面代码,启动mysql服务:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker-compose")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" docker-compose.yaml up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-创建mod和配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-创建mod和配置"}},[t._v("#")]),t._v(" 2. 创建mod和配置")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" use-rdb\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创建mod")]),t._v("\ngo mod init use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创建数据库配置")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("cat")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),t._v(" config/default.properties"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("eof\n\n# 配置使用mysql数据库\ndatabase.driver-name=mysql\n\n#数据源\ndatabase.dsn=root:123456@tcp(localhost:3306)/demo?charset=utf8mb4&loc=Local\neof")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-编写代码如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-编写代码如下"}},[t._v("#")]),t._v(" 3. 编写代码如下:")]),t._v(" "),s("p",[t._v("文件名:main.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v(" #⭐️需要特别注意,需要引入mysql的驱动\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #⭐️需要特别注意,导入的是gone封装的xorm包\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v("\n\tData "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SqlExecutor "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdb xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 同步表结构")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sync")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("new")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tdemo "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello gone"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 插入数据")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Insert")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" \n Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. "')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo: %v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" list "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Demo\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 读取数据")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Find")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo records:%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 通过 goner.XormPriest 将xorm相关的Goner埋葬到 cemetery")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 在Gone启动完成后执行")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\te SqlExecutor "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-运行代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-运行代码"}},[t._v("#")]),t._v(" 3. 运行代码")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("运行结果如下:")]),t._v(" "),s("div",{staticClass:"language-log extra-class"},[s("div",{staticClass:"highlight-lines"},[s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("br")]),s("pre",{pre:!0,attrs:{class:"language-log"}},[s("code",[s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.767")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("heaven\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("cemetery\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" local\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/default.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/example/use-rdb/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("xorm"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("engine\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive main"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("SqlExecutor\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.769")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("90")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.783")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/cemetery.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("307")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("\ndemo"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v("\ndemo records"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v(" The most Spring programmer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("friendly Golang framework"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" dependency injection"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" integrates Web"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n")])])]),s("h2",{attrs:{id:"需要注意的点"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#需要注意的点"}},[t._v("#")]),t._v(" 需要注意的点")]),t._v(" "),s("h3",{attrs:{id:"_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[t._v("#")]),t._v(" 1. "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" 是对 "),s("code",[t._v("xorm.io/xorm")]),t._v(" 的封装")]),t._v(" "),s("p",[t._v("封装的大概逻辑是:将xorm包装为一个"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#%F0%9F%98%87angel-%E5%A4%A9%E4%BD%BF",target:"_blank",rel:"noopener noreferrer"}},[t._v("Angel Goner"),s("OutboundLink")],1),t._v(";在Start方法中完成数据的连接;在Stop方法中关闭数据库连接。使用时不需要导入"),s("code",[t._v("xorm.io/xorm")]),t._v("包,只需要导入"),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v("即可。封装中依赖了"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner/config")]),t._v("来实现配置,在"),s("code",[t._v("goner.XormPriest")]),t._v("函数中已经自动完成了配置相关的Goner的"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC",target:"_blank",rel:"noopener noreferrer"}},[t._v("埋葬"),s("OutboundLink")],1),t._v("。\n配置文件中支持的配置项:")]),t._v(" "),s("ul",[s("li",[t._v("database.driver-name 驱动的名称,支持 mysql、postgres 等主流数据库,具体可以参考 "),s("a",{attrs:{href:"https://xorm.io/zh/docs/chapter-01/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1)]),t._v(" "),s("li",[t._v("database.dsn 数据源,参考 "),s("a",{attrs:{href:"https://xorm.io/zh/docs/chapter-01/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1)]),t._v(" "),s("li",[t._v("database.max-idle-count 连接池,最大空闲连接数")]),t._v(" "),s("li",[t._v("database.max-open 连接池,最大连接数")]),t._v(" "),s("li",[t._v("database.max-lifetime 最大生存时间")]),t._v(" "),s("li",[t._v("database.showSql 是否打印执行的SQL")])]),t._v(" "),s("h3",{attrs:{id:"_2-通过gone-xormgonerid注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-通过gone-xormgonerid注入"}},[t._v("#")]),t._v(" 2. 通过"),s("code",[t._v("gone-xorm")]),s("strong",[t._v("GonerId")]),t._v("注入")]),t._v(" "),s("p",[t._v("在需要使用xorm引擎的Goner中,使用"),s("code",[t._v('gone:"gone-xorm"')]),t._v("标签注入"),s("code",[t._v("xorm.Engine")]),t._v(",属性可以具名也可以匿名:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名")]),t._v("\n db xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名")]),t._v("\n xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在"),s("code",[t._v("xorm.Engine")]),t._v("中“继承”了"),s("code",[t._v("xorm.io/xorm.EngineInterface")]),t._v("接口,所以"),s("code",[t._v("xorm.Engine")]),t._v("支持"),s("a",{attrs:{href:"https://xorm.io/zh/docs/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1),t._v("中的所有方法。")]),t._v(" "),s("h3",{attrs:{id:"_3-注意导入的包"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-注意导入的包"}},[t._v("#")]),t._v(" 3. 注意导入的包")]),t._v(" "),s("ul",[s("li",[t._v("需要导入数据库的驱动,不同数据库导入不一样:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("ul",[s("li",[t._v("导入的xorm包是:"),s("code",[t._v("github.com/gone-io/gone/goner/xorm")]),t._v(",不是"),s("code",[t._v("xorm.io/xorm")]),t._v(":")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #⭐️需要特别注意,导入的是gone封装的xorm包\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[63],{353:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"通过内置goners支持数据库操作"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#通过内置goners支持数据库操作"}},[t._v("#")]),t._v(" 通过内置Goners支持数据库操作")]),t._v(" "),s("p"),s("div",{staticClass:"table-of-contents"},[s("ul",[s("li",[s("a",{attrs:{href:"#以mysql举个例子-源代码在-这里-https-github-com-gone-io-gone-tree-main-example-use-rdb-可以找到"}},[t._v("以Mysql举个例子,源代码在这里可以找到")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[t._v("1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务")])]),s("li",[s("a",{attrs:{href:"#_2-创建mod和配置"}},[t._v("2. 创建mod和配置")])]),s("li",[s("a",{attrs:{href:"#_3-编写代码如下"}},[t._v("3. 编写代码如下:")])]),s("li",[s("a",{attrs:{href:"#_3-运行代码"}},[t._v("3. 运行代码")])])])]),s("li",[s("a",{attrs:{href:"#需要注意的点"}},[t._v("需要注意的点")]),s("ul",[s("li",[s("a",{attrs:{href:"#_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[t._v("1. github.com/gone-io/gone/goner 是对 xorm.io/xorm 的封装")])]),s("li",[s("a",{attrs:{href:"#_2-通过-gone-xorm-gonerid-注入"}},[t._v("2. 通过gone-xormGonerId注入")])]),s("li",[s("a",{attrs:{href:"#_3-注意导入的包"}},[t._v("3. 注意导入的包")])])])])])]),s("p"),t._v(" "),s("h2",{attrs:{id:"以mysql举个例子-源代码在这里可以找到"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#以mysql举个例子-源代码在这里可以找到"}},[t._v("#")]),t._v(" 以Mysql举个例子,源代码在"),s("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example/use-rdb",target:"_blank",rel:"noopener noreferrer"}},[t._v("这里"),s("OutboundLink")],1),t._v("可以找到")]),t._v(" "),s("h3",{attrs:{id:"_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"}},[t._v("#")]),t._v(" 1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务")]),t._v(" "),s("p",[t._v("docker-compose.yaml 内容如下:")]),t._v(" "),s("div",{staticClass:"language-yaml extra-class"},[s("pre",{pre:!0,attrs:{class:"language-yaml"}},[s("code",[s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("version")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3.5"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("services")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("mysql")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("image")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("8.3")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("restart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" unless"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v("stopped\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("environment")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_ROOT_PASSWORD")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("123456")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("MYSQL_DATABASE")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" demo\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("volumes")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" ./data/mysql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("/var/lib/mysql\n "),s("span",{pre:!0,attrs:{class:"token key atrule"}},[t._v("ports")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("-")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"3306:3306"')]),t._v("\n")])])]),s("p",[t._v("命令行执行下面代码,启动mysql服务:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("docker-compose")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" docker-compose.yaml up "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-d")]),t._v("\n")])])]),s("h3",{attrs:{id:"_2-创建mod和配置"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-创建mod和配置"}},[t._v("#")]),t._v(" 2. 创建mod和配置")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" use-rdb\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创建mod")]),t._v("\ngo mod init use-rdb\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创建数据库配置")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("mkdir")]),t._v(" config\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("cat")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">>")]),t._v(" config/default.properties"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("<<")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("eof\n\n# 配置使用mysql数据库\ndatabase.driver-name=mysql\n\n#数据源\ndatabase.dsn=root:123456@tcp(localhost:3306)/demo?charset=utf8mb4&loc=Local\neof")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-编写代码如下"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-编写代码如下"}},[t._v("#")]),t._v(" 3. 编写代码如下:")]),t._v(" "),s("p",[t._v("文件名:main.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v(" #⭐️需要特别注意,需要引入mysql的驱动\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #⭐️需要特别注意,导入的是gone封装的xorm包\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Demo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),t._v("\n\tData "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" SqlExecutor "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdb xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("e "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 同步表结构")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sync")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("new")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tdemo "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello gone"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 插入数据")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Insert")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" \n Demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("Data"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"The most Spring programmer-friendly Golang framework, dependency injection, integrates Web. "')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo: %v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" demo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" list "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("Demo\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 读取数据")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Find")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"demo records:%v\\n"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" list"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 通过 goner.XormPriest 将xorm相关的Goner埋葬到 cemetery")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("XormPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("SqlExecutor"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 在Gone启动完成后执行")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\te SqlExecutor "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("e"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Execute")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"_3-运行代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-运行代码"}},[t._v("#")]),t._v(" 3. 运行代码")]),t._v(" "),s("p",[t._v("执行如下命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go mod tidy\ngo run main.go\n")])])]),s("p",[t._v("运行结果如下:")]),t._v(" "),s("div",{staticClass:"language-log extra-class"},[s("div",{staticClass:"highlight-lines"},[s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("br"),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("div",{staticClass:"highlighted"},[t._v(" ")]),s("br")]),s("pre",{pre:!0,attrs:{class:"language-log"}},[s("code",[s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.767")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("heaven\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("cemetery\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("tracer\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logrus"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("logger\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(">")]),t._v("Use Env"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" local\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/default.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/var/folders/jv/rn9b7nhs2ls1n1j_lqj005r80000gn/T/go-build1582606196/b001/exe/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level warning important"}},[t._v("WARNING")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token property"}},[t._v("properties:")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/example/use-rdb/config/local.properties")]),t._v(" not found"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" skipping\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("config"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("propertiesConfigure\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive github"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("com"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("io"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("goner"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("xorm"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("engine\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.768")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Init"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive main"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("SqlExecutor\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.769")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/go/pkg/mod/xorm.io/xorm@v1.3.2/log/logger_context.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("90")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("PING DATABASE mysql\n"),s("span",{pre:!0,attrs:{class:"token date number"}},[t._v("2024-05-10")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token time number"}},[t._v("16:56:52.783")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token level info keyword"}},[t._v("INFO")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token file-path string"}},[t._v("/Users/jim/works/gone-io/gone/cemetery.go")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("307")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("|")]),t._v("Revive "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("/")]),t._v("\ndemo"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v("\ndemo records"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),t._v(" hello gone"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("2")]),t._v(" The most Spring programmer"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("-")]),t._v("friendly Golang framework"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" dependency injection"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" integrates Web"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n")])])]),s("h2",{attrs:{id:"需要注意的点"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#需要注意的点"}},[t._v("#")]),t._v(" 需要注意的点")]),t._v(" "),s("h3",{attrs:{id:"_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"}},[t._v("#")]),t._v(" 1. "),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v(" 是对 "),s("code",[t._v("xorm.io/xorm")]),t._v(" 的封装")]),t._v(" "),s("p",[t._v("封装的大概逻辑是:将xorm包装为一个"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#%F0%9F%98%87angel-%E5%A4%A9%E4%BD%BF",target:"_blank",rel:"noopener noreferrer"}},[t._v("Angel Goner"),s("OutboundLink")],1),t._v(";在Start方法中完成数据的连接;在Stop方法中关闭数据库连接。使用时不需要导入"),s("code",[t._v("xorm.io/xorm")]),t._v("包,只需要导入"),s("code",[t._v("github.com/gone-io/gone/goner")]),t._v("即可。封装中依赖了"),s("code",[t._v("github.com/gone-io/gone/tree/main/goner/config")]),t._v("来实现配置,在"),s("code",[t._v("goner.XormPriest")]),t._v("函数中已经自动完成了配置相关的Goner的"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC",target:"_blank",rel:"noopener noreferrer"}},[t._v("埋葬"),s("OutboundLink")],1),t._v("。\n配置文件中支持的配置项:")]),t._v(" "),s("ul",[s("li",[t._v("database.driver-name 驱动的名称,支持 mysql、postgres 等主流数据库,具体可以参考 "),s("a",{attrs:{href:"https://xorm.io/zh/docs/chapter-01/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1)]),t._v(" "),s("li",[t._v("database.dsn 数据源,参考 "),s("a",{attrs:{href:"https://xorm.io/zh/docs/chapter-01/1.engine/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1)]),t._v(" "),s("li",[t._v("database.max-idle-count 连接池,最大空闲连接数")]),t._v(" "),s("li",[t._v("database.max-open 连接池,最大连接数")]),t._v(" "),s("li",[t._v("database.max-lifetime 最大生存时间")]),t._v(" "),s("li",[t._v("database.showSql 是否打印执行的SQL")])]),t._v(" "),s("h3",{attrs:{id:"_2-通过gone-xormgonerid注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-通过gone-xormgonerid注入"}},[t._v("#")]),t._v(" 2. 通过"),s("code",[t._v("gone-xorm")]),s("strong",[t._v("GonerId")]),t._v("注入")]),t._v(" "),s("p",[t._v("在需要使用xorm引擎的Goner中,使用"),s("code",[t._v('gone:"gone-xorm"')]),t._v("标签注入"),s("code",[t._v("xorm.Engine")]),t._v(",属性可以具名也可以匿名:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//具名")]),t._v("\n db xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 或者")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" XormUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//匿名")]),t._v("\n xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Engine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在"),s("code",[t._v("xorm.Engine")]),t._v("中“继承”了"),s("code",[t._v("xorm.io/xorm.EngineInterface")]),t._v("接口,所以"),s("code",[t._v("xorm.Engine")]),t._v("支持"),s("a",{attrs:{href:"https://xorm.io/zh/docs/",target:"_blank",rel:"noopener noreferrer"}},[t._v("xorm文档"),s("OutboundLink")],1),t._v("中的所有方法。")]),t._v(" "),s("h3",{attrs:{id:"_3-注意导入的包"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-注意导入的包"}},[t._v("#")]),t._v(" 3. 注意导入的包")]),t._v(" "),s("ul",[s("li",[t._v("需要导入数据库的驱动,不同数据库导入不一样:")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/go-sql-driver/mysql"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("ul",[s("li",[t._v("导入的xorm包是:"),s("code",[t._v("github.com/gone-io/gone/goner/xorm")]),t._v(",不是"),s("code",[t._v("xorm.io/xorm")]),t._v(":")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/xorm"')]),t._v(" #⭐️需要特别注意,导入的是gone封装的xorm包\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/64.b476c284.js b/assets/js/64.6b7e70bd.js similarity index 95% rename from assets/js/64.b476c284.js rename to assets/js/64.6b7e70bd.js index 7b4ff5f..1cd6976 100644 --- a/assets/js/64.b476c284.js +++ b/assets/js/64.6b7e70bd.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[64],{354:function(t,r,a){"use strict";a.r(r);var e=a(10),s=Object(e.a)({},(function(){var t=this,r=t._self._c;return r("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[r("h1",{attrs:{id:"快速开始"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#快速开始"}},[t._v("#")]),t._v(" 快速开始")]),t._v(" "),r("h2",{attrs:{id:"创建一个简单的web项目"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#创建一个简单的web项目"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/quick-start/simple.html"}},[t._v("创建一个简单的Web项目")])],1),t._v(" "),r("h2",{attrs:{id:"创建一个使用mysql做数据库-可以应用到生产环境的项目"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#创建一个使用mysql做数据库-可以应用到生产环境的项目"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/quick-start/simple.html"}},[t._v("创建一个使用MySql做数据库,可以应用到生产环境的项目")])],1),t._v(" "),r("h2",{attrs:{id:"更多例子"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#更多例子"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example",target:"_blank",rel:"noopener noreferrer"}},[t._v("更多例子"),r("OutboundLink")],1)])])}),[],!1,null,null,null);r.default=s.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[64],{352:function(t,r,a){"use strict";a.r(r);var e=a(10),s=Object(e.a)({},(function(){var t=this,r=t._self._c;return r("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[r("h1",{attrs:{id:"快速开始"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#快速开始"}},[t._v("#")]),t._v(" 快速开始")]),t._v(" "),r("h2",{attrs:{id:"创建一个简单的web项目"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#创建一个简单的web项目"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/quick-start/simple.html"}},[t._v("创建一个简单的Web项目")])],1),t._v(" "),r("h2",{attrs:{id:"创建一个使用mysql做数据库-可以应用到生产环境的项目"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#创建一个使用mysql做数据库-可以应用到生产环境的项目"}},[t._v("#")]),t._v(" "),r("RouterLink",{attrs:{to:"/zh/quick-start/simple.html"}},[t._v("创建一个使用MySql做数据库,可以应用到生产环境的项目")])],1),t._v(" "),r("h2",{attrs:{id:"更多例子"}},[r("a",{staticClass:"header-anchor",attrs:{href:"#更多例子"}},[t._v("#")]),t._v(" "),r("a",{attrs:{href:"https://github.com/gone-io/gone/tree/main/example",target:"_blank",rel:"noopener noreferrer"}},[t._v("更多例子"),r("OutboundLink")],1)])])}),[],!1,null,null,null);r.default=s.exports}}]); \ No newline at end of file diff --git a/assets/js/65.112732bb.js b/assets/js/65.b74504b8.js similarity index 99% rename from assets/js/65.112732bb.js rename to assets/js/65.b74504b8.js index 3cdfc98..f0073eb 100644 --- a/assets/js/65.112732bb.js +++ b/assets/js/65.b74504b8.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[65],{353:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"封装一个goner调用gpt"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#封装一个goner调用gpt"}},[t._v("#")]),t._v(" 封装一个Goner调用GPT")]),t._v(" "),n("p",[t._v("下面例子的代码已经开源在:https://github.com/gone-io/gpt;如果需要,可以导入使用:"),n("code",[t._v("go get -u github.com/gone-io/gpt")]),t._v("。")]),t._v(" "),n("h2",{attrs:{id:"定义用于依赖注入的接口"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#定义用于依赖注入的接口"}},[t._v("#")]),t._v(" 定义用于依赖注入的接口")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" ChatGPT "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"实现这个接口并嵌入gone-flag"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#实现这个接口并嵌入gone-flag"}},[t._v("#")]),t._v(" 实现这个接口并嵌入"),n("code",[t._v("gone.Flag")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("上面代码,通过嵌入"),n("code",[t._v("*openai.Client")]),t._v(",实现了"),n("code",[t._v("ChatGPT")]),t._v("接口,同时通过嵌入"),n("code",[t._v("gone.Flag")]),t._v("实现了Goner接口,使得这个结构体可以注入到依赖注入中。")]),t._v(" "),n("h2",{attrs:{id:"设计配置项"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#设计配置项"}},[t._v("#")]),t._v(" 设计配置项")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("openai.base")]),t._v(":配置OpenAI的API地址")]),t._v(" "),n("li",[n("code",[t._v("openai.token")]),t._v(":配置OpenAI的API密钥")])]),t._v(" "),n("p",[t._v("我们希望可以将这两个配置注入到我们实现的"),n("code",[t._v("chatGPTClient")]),t._v("结构体中,如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.base`配置项")]),t._v("\n\topenaiBase "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.base"`')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.token`配置项")]),t._v("\n\topenaiToken "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.token"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"使chatgptclient实现接口afterrevive-error函数-称为-先知"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#使chatgptclient实现接口afterrevive-error函数-称为-先知"}},[t._v("#")]),t._v(" 使chatGPTClient实现接口"),n("code",[t._v("AfterRevive() error")]),t._v("函数,称为“先知”")]),t._v(" "),n("p",[n("code",[t._v("AfterRevive")]),t._v("方法会在"),n("code",[t._v("gone")]),t._v("框架初始化时调用,用于初始化"),n("code",[t._v("chatGPTClient")]),t._v("结构体中的"),n("code",[t._v("*openai.Client")]),t._v("字段。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tconf "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("DefaultConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiToken"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tconf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BaseURL "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiBase\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewClientWithConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("好了,到此我们就基本完成了我们的目标:封装一个Goner用于调用GPT。")]),t._v(" "),n("p",[t._v("但是为了方便用户使用我们还需要做下面工作。")]),t._v(" "),n("h2",{attrs:{id:"方便用户使用"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#方便用户使用"}},[t._v("#")]),t._v(" 方便用户使用")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("定义用于注入的键值,方便用户按GonerId注入\n我们这里定义为:"),n("code",[t._v("gone-gpt")])])]),t._v(" "),n("li",[n("p",[t._v("为了方便用户埋葬这个Goner,我们定义一个开放的New函数")])])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewChatGPTClient returns a new Goner which is ChatGPT client.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gpt"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("ol",{attrs:{start:"3"}},[n("li",[t._v("前面我们用到了配置注入,我们实际上依赖了Gone框架中的配置模块,我们希望用户在埋葬我们定义chatGPTClient时能顺便将配置相关的Goner也埋葬了,所以我们再定义一个Priest函数,如下:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest 用于埋葬chatGPTClient和其依赖的Goner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用config.Priest来埋葬Gone配置模块相关的Goner")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬chatGPTClient")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"完整的疯转代码如下"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#完整的疯转代码如下"}},[t._v("#")]),t._v(" 完整的疯转代码如下")]),t._v(" "),n("p",[t._v("文件名:gpt.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gpt\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"context"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/sashabaranov/go-openai"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" ChatGPT "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewChatGPTClient returns a new Goner which is ChatGPT client.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gpt"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest 用于埋葬chatGPTClient和其依赖的Goner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用config.Priest来埋葬Gone配置模块相关的Goner")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬chatGPTClient")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.base`配置项")]),t._v("\n\topenaiBase "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.base"`')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.token`配置项")]),t._v("\n\topenaiToken "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.token"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tconf "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("DefaultConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiToken"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tconf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BaseURL "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiBase\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewClientWithConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"编写测试"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#编写测试"}},[t._v("#")]),t._v(" 编写测试")]),t._v(" "),n("p",[t._v("建立测试文件: "),n("code",[t._v("gpt_test.go")]),t._v("\n内容如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gpt\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gpt "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NotNil")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gpt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("实际上,我们的封装只是做了对openai.Client的初始化,我们只需要测试配置是否正确即可。")]),t._v(" "),n("h2",{attrs:{id:"使用和编写示例"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#使用和编写示例"}},[t._v("#")]),t._v(" 使用和编写示例")]),t._v(" "),n("ol",[n("li",[t._v("在配置文件中,写入配置项,参考"),n("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),n("OutboundLink")],1)]),t._v(" "),n("li",[t._v("在需要使用的结构体中注入ChatGPT并调用相关接口,代码示例如下:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"context"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gpt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/sashabaranov/go-openai"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Chat "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgPT gpt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatGPT "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gpt"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Chat"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Use")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ask "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tresponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("gPT"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TODO")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tModel"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GPT3Dot5Turbo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\tMessages"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionMessage"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tRole"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatMessageRoleSystem"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t\tContent"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"you are a helpful chatbot"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tRole"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatMessageRoleUser"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t\tContent"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" ask"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" err\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Choices"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Message"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[65],{357:function(t,n,s){"use strict";s.r(n);var a=s(10),e=Object(a.a)({},(function(){var t=this,n=t._self._c;return n("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[n("h1",{attrs:{id:"封装一个goner调用gpt"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#封装一个goner调用gpt"}},[t._v("#")]),t._v(" 封装一个Goner调用GPT")]),t._v(" "),n("p",[t._v("下面例子的代码已经开源在:https://github.com/gone-io/gpt;如果需要,可以导入使用:"),n("code",[t._v("go get -u github.com/gone-io/gpt")]),t._v("。")]),t._v(" "),n("h2",{attrs:{id:"定义用于依赖注入的接口"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#定义用于依赖注入的接口"}},[t._v("#")]),t._v(" 定义用于依赖注入的接口")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" ChatGPT "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"实现这个接口并嵌入gone-flag"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#实现这个接口并嵌入gone-flag"}},[t._v("#")]),t._v(" 实现这个接口并嵌入"),n("code",[t._v("gone.Flag")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("上面代码,通过嵌入"),n("code",[t._v("*openai.Client")]),t._v(",实现了"),n("code",[t._v("ChatGPT")]),t._v("接口,同时通过嵌入"),n("code",[t._v("gone.Flag")]),t._v("实现了Goner接口,使得这个结构体可以注入到依赖注入中。")]),t._v(" "),n("h2",{attrs:{id:"设计配置项"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#设计配置项"}},[t._v("#")]),t._v(" 设计配置项")]),t._v(" "),n("ul",[n("li",[n("code",[t._v("openai.base")]),t._v(":配置OpenAI的API地址")]),t._v(" "),n("li",[n("code",[t._v("openai.token")]),t._v(":配置OpenAI的API密钥")])]),t._v(" "),n("p",[t._v("我们希望可以将这两个配置注入到我们实现的"),n("code",[t._v("chatGPTClient")]),t._v("结构体中,如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.base`配置项")]),t._v("\n\topenaiBase "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.base"`')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.token`配置项")]),t._v("\n\topenaiToken "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.token"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"使chatgptclient实现接口afterrevive-error函数-称为-先知"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#使chatgptclient实现接口afterrevive-error函数-称为-先知"}},[t._v("#")]),t._v(" 使chatGPTClient实现接口"),n("code",[t._v("AfterRevive() error")]),t._v("函数,称为“先知”")]),t._v(" "),n("p",[n("code",[t._v("AfterRevive")]),t._v("方法会在"),n("code",[t._v("gone")]),t._v("框架初始化时调用,用于初始化"),n("code",[t._v("chatGPTClient")]),t._v("结构体中的"),n("code",[t._v("*openai.Client")]),t._v("字段。")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tconf "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("DefaultConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiToken"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tconf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BaseURL "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiBase\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewClientWithConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("好了,到此我们就基本完成了我们的目标:封装一个Goner用于调用GPT。")]),t._v(" "),n("p",[t._v("但是为了方便用户使用我们还需要做下面工作。")]),t._v(" "),n("h2",{attrs:{id:"方便用户使用"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#方便用户使用"}},[t._v("#")]),t._v(" 方便用户使用")]),t._v(" "),n("ol",[n("li",[n("p",[t._v("定义用于注入的键值,方便用户按GonerId注入\n我们这里定义为:"),n("code",[t._v("gone-gpt")])])]),t._v(" "),n("li",[n("p",[t._v("为了方便用户埋葬这个Goner,我们定义一个开放的New函数")])])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewChatGPTClient returns a new Goner which is ChatGPT client.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gpt"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("ol",{attrs:{start:"3"}},[n("li",[t._v("前面我们用到了配置注入,我们实际上依赖了Gone框架中的配置模块,我们希望用户在埋葬我们定义chatGPTClient时能顺便将配置相关的Goner也埋葬了,所以我们再定义一个Priest函数,如下:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest 用于埋葬chatGPTClient和其依赖的Goner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用config.Priest来埋葬Gone配置模块相关的Goner")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬chatGPTClient")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"完整的疯转代码如下"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#完整的疯转代码如下"}},[t._v("#")]),t._v(" 完整的疯转代码如下")]),t._v(" "),n("p",[t._v("文件名:gpt.go")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gpt\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"context"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/config"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/sashabaranov/go-openai"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" ChatGPT "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("CompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tctx context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\trequest openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionResponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// NewChatGPTClient returns a new Goner which is ChatGPT client.")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-gpt"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Priest 用于埋葬chatGPTClient和其依赖的Goner")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用config.Priest来埋葬Gone配置模块相关的Goner")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" config"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Priest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//埋葬chatGPTClient")]),t._v("\n\tcemetery"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewChatGPTClient")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" chatGPTClient "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\t"),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.base`配置项")]),t._v("\n\topenaiBase "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.base"`')]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//从配置文件中注入`openai.token`配置项")]),t._v("\n\topenaiToken "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"config,openai.token"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tconf "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("DefaultConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiToken"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tconf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("BaseURL "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" g"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("openaiBase\n\tg"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewClientWithConfig")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conf"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("h2",{attrs:{id:"编写测试"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#编写测试"}},[t._v("#")]),t._v(" 编写测试")]),t._v(" "),n("p",[t._v("建立测试文件: "),n("code",[t._v("gpt_test.go")]),t._v("\n内容如下:")]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" gpt\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/stretchr/testify/assert"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"testing"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TestPriest")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("testing"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("T"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Test")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gpt "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("chatGPTClient"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tassert"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("NotNil")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gpt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Client"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" Priest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),n("p",[t._v("实际上,我们的封装只是做了对openai.Client的初始化,我们只需要测试配置是否正确即可。")]),t._v(" "),n("h2",{attrs:{id:"使用和编写示例"}},[n("a",{staticClass:"header-anchor",attrs:{href:"#使用和编写示例"}},[t._v("#")]),t._v(" 使用和编写示例")]),t._v(" "),n("ol",[n("li",[t._v("在配置文件中,写入配置项,参考"),n("a",{attrs:{href:"https://goner.fun/zh/guide/config.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("通过内置Goners支持配置文件"),n("OutboundLink")],1)]),t._v(" "),n("li",[t._v("在需要使用的结构体中注入ChatGPT并调用相关接口,代码示例如下:")])]),t._v(" "),n("div",{staticClass:"language-go extra-class"},[n("pre",{pre:!0,attrs:{class:"language-go"}},[n("code",[n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" example\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"context"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gpt"')]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/sashabaranov/go-openai"')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Chat "),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgPT gpt"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatGPT "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gpt"`')]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("c "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Chat"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("Use")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ask "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tresponse"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" c"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("gPT"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("CreateChatCompletion")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("context"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("TODO")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionRequest"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tModel"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GPT3Dot5Turbo"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\tMessages"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatCompletionMessage"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tRole"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatMessageRoleSystem"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t\tContent"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token string"}},[t._v('"you are a helpful chatbot"')]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tRole"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" openai"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ChatMessageRoleUser"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t\tContent"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" ask"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),n("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" err\n\t"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),n("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("response"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Choices"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),n("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Message"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),n("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),n("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),n("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);n.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/66.2fd76718.js b/assets/js/66.415590d1.js similarity index 99% rename from assets/js/66.2fd76718.js rename to assets/js/66.415590d1.js index 1d30d48..e7a2028 100644 --- a/assets/js/66.2fd76718.js +++ b/assets/js/66.415590d1.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[66],{355:function(t,s,a){"use strict";a.r(s);var e=a(10),r=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"jwt-mysql-redis-schedule"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#jwt-mysql-redis-schedule"}},[t._v("#")]),t._v(" JWT + MySQL + Redis + Schedule")]),t._v(" "),s("p",[t._v("在本章节中,我们将在"),s("a",{attrs:{href:"https://goner.fun/zh/quick-start/mysql.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("上一个例子"),s("OutboundLink")],1),t._v("的基础上增加:使用JWT鉴权,Redis做缓存,集成定时任务的Web项目。")]),t._v(" "),s("h2",{attrs:{id:"业务和接口说明"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#业务和接口说明"}},[t._v("#")]),t._v(" 业务和接口说明")]),t._v(" "),s("p",[t._v("在这个例子中,我们将实现如下功能和接口:")]),t._v(" "),s("ol",[s("li",[t._v("我们将用户分为管理员和普通用户两种角色,管理员可以管理用户,普通用户只能查看自己的信息。")])]),t._v(" "),s("p",[t._v("下面是普通用户的接口:")]),t._v(" "),s("ul",[s("li",[s("p",[t._v("用户注册接口")]),t._v(" "),s("ul",[s("li",[t._v("请求")])]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("POST")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/register")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"password"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"email"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin@goner.fun"')]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"verifyCode"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),s("ul",[s("li",[t._v("应答\n"),s("ul",[s("li",[t._v("状态码:200")]),t._v(" "),s("li",[t._v("返回内容"),s("div",{staticClass:"language-json extra-class"},[s("pre",{pre:!0,attrs:{class:"language-json"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"code"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"id"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])])])])]),t._v(" "),s("li",[s("p",[t._v("用户登录接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("POST")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/login")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"password"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])]),t._v(" "),s("li",[s("p",[t._v("获取当前用户信息接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("GET")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/info")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Authorization")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("Bearer ")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n")])])])]),t._v(" "),s("li",[s("p",[t._v("修改当前用户信息接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("PUT")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/info")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Authorization")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("Bearer ")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"email"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin@goner.fun"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")])])])])])])])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[66],{358:function(t,s,a){"use strict";a.r(s);var e=a(10),r=Object(e.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"jwt-mysql-redis-schedule"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#jwt-mysql-redis-schedule"}},[t._v("#")]),t._v(" JWT + MySQL + Redis + Schedule")]),t._v(" "),s("p",[t._v("在本章节中,我们将在"),s("a",{attrs:{href:"https://goner.fun/zh/quick-start/mysql.html",target:"_blank",rel:"noopener noreferrer"}},[t._v("上一个例子"),s("OutboundLink")],1),t._v("的基础上增加:使用JWT鉴权,Redis做缓存,集成定时任务的Web项目。")]),t._v(" "),s("h2",{attrs:{id:"业务和接口说明"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#业务和接口说明"}},[t._v("#")]),t._v(" 业务和接口说明")]),t._v(" "),s("p",[t._v("在这个例子中,我们将实现如下功能和接口:")]),t._v(" "),s("ol",[s("li",[t._v("我们将用户分为管理员和普通用户两种角色,管理员可以管理用户,普通用户只能查看自己的信息。")])]),t._v(" "),s("p",[t._v("下面是普通用户的接口:")]),t._v(" "),s("ul",[s("li",[s("p",[t._v("用户注册接口")]),t._v(" "),s("ul",[s("li",[t._v("请求")])]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("POST")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/register")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"password"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"email"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin@goner.fun"')]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token string-property property"}},[t._v('"verifyCode"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])]),s("ul",[s("li",[t._v("应答\n"),s("ul",[s("li",[t._v("状态码:200")]),t._v(" "),s("li",[t._v("返回内容"),s("div",{staticClass:"language-json extra-class"},[s("pre",{pre:!0,attrs:{class:"language-json"}},[s("code",[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"code"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("0")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"data"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"id"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])])])])]),t._v(" "),s("li",[s("p",[t._v("用户登录接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("POST")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/login")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"password"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"123456"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n")])])])])]),t._v(" "),s("li",[s("p",[t._v("获取当前用户信息接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("GET")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/info")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Authorization")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("Bearer ")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n")])])])]),t._v(" "),s("li",[s("p",[t._v("修改当前用户信息接口")]),t._v(" "),s("div",{staticClass:"language-http extra-class"},[s("pre",{pre:!0,attrs:{class:"language-http"}},[s("code",[s("span",{pre:!0,attrs:{class:"token request-line"}},[s("span",{pre:!0,attrs:{class:"token method property"}},[t._v("PUT")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token request-target url"}},[t._v("/api/users/info")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token http-version property"}},[t._v("HTTP/1.1")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Authorization")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("Bearer ")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token header"}},[s("span",{pre:!0,attrs:{class:"token header-name keyword"}},[t._v("Content-Type")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token header-value"}},[t._v("application/json")])]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token application-json"}},[s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"username"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token property"}},[t._v('"email"')]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"admin@goner.fun"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")])])])])])])])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file diff --git a/assets/js/67.9f23e1a7.js b/assets/js/67.5d3c2943.js similarity index 99% rename from assets/js/67.9f23e1a7.js rename to assets/js/67.5d3c2943.js index b37ead1..e0f2a87 100644 --- a/assets/js/67.9f23e1a7.js +++ b/assets/js/67.5d3c2943.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[67],{357:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"一个极简例子"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#一个极简例子"}},[t._v("#")]),t._v(" 一个极简例子")]),t._v(" "),s("p",[t._v("代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("w "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker do"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n\tseller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Worker")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("b "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss do"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tb"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("seller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//AfterStart 是一个hook函数,关于hook函数请参考文档:https://goner.fun/zh/guide/hooks.html")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tboss "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Boss "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Boss")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在这个例子中,在Gone的准备阶段,通过"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC",target:"_blank",rel:"noopener noreferrer"}},[s("strong",[t._v("埋葬")]),s("OutboundLink")],1),t._v("的方式将 Boss 和 Worker 导入到框架。在框架启动后,老板开始工作"),s("code",[t._v("in.boss.Do()")]),t._v(";老板工作中调用销售开始工作"),s("code",[t._v("b.seller.Do()")]),t._v("。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[67],{359:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"一个极简例子"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#一个极简例子"}},[t._v("#")]),t._v(" 一个极简例子")]),t._v(" "),s("p",[t._v("代码如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" main\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"fmt"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Worker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("w "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"worker do"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Boss "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner")]),t._v("\n\n\tseller "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Worker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Worker")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("b "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"boss do"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\tb"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("seller"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("main")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prepare")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tcemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bury")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("Worker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//AfterStart 是一个hook函数,关于hook函数请参考文档:https://goner.fun/zh/guide/hooks.html")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterStart")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tboss "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Boss "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入Boss")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\tin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("boss"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Run")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("在这个例子中,在Gone的准备阶段,通过"),s("a",{attrs:{href:"https://goner.fun/zh/guide/core-concept.html#bury-%E5%9F%8B%E8%91%AC",target:"_blank",rel:"noopener noreferrer"}},[s("strong",[t._v("埋葬")]),s("OutboundLink")],1),t._v("的方式将 Boss 和 Worker 导入到框架。在框架启动后,老板开始工作"),s("code",[t._v("in.boss.Do()")]),t._v(";老板工作中调用销售开始工作"),s("code",[t._v("b.seller.Do()")]),t._v("。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/68.a6f51e26.js b/assets/js/68.9b5e8a8c.js similarity index 99% rename from assets/js/68.a6f51e26.js rename to assets/js/68.9b5e8a8c.js index 0ee3f01..8ab955d 100644 --- a/assets/js/68.a6f51e26.js +++ b/assets/js/68.9b5e8a8c.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[68],{356:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-项目"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-项目"}},[t._v("#")]),t._v(" Web 项目")]),t._v(" "),s("h2",{attrs:{id:"安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#安装gone辅助工具"}},[t._v("#")]),t._v(" 安装gone辅助工具")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("blockquote",[s("p",[t._v("安装可以使用gone命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n")])])]),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("\t➜ demo gone "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n\tNAME:\n\tgone - A new cli application\n\n\tUSAGE:\n\tgone "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("global options"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("command options"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("arguments"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v("."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n\tDESCRIPTION:\n\tgenerate gone code or generate gone app\n\n\tCOMMANDS:\n\tpriest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${scanPackageDir}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${pkgName}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${funcName}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outputFilePath}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-w"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\tmock "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n\tcreate "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-t "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${template}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-m "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${modName}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${appName}")]),t._v("\n\thelp, h Shows a list of commands or "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" one "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v("\n\n\tGLOBAL OPTIONS:\n\t--help, "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v(" show "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("default: "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("false")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("支持的功能:")]),t._v(" "),s("ol",[s("li",[t._v("create,创建一个gone app,暂时只支持创建web app")]),t._v(" "),s("li",[t._v("priest,为项目自动生成 "),s("strong",[t._v("Priest")]),t._v(" 函数,"),s("RouterLink",{attrs:{to:"/zh/"}},[t._v("了解更多")])],1),t._v(" "),s("li",[t._v("生成用于测试的mock代码")])])]),t._v(" "),s("h2",{attrs:{id:"创建一个web项目并运行代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#创建一个web项目并运行代码"}},[t._v("#")]),t._v(" 创建一个web项目并运行代码")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创将一个名为 web-app 的项目")]),t._v("\ngone create web-app\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-app\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("h2",{attrs:{id:"项目结构"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目结构"}},[t._v("#")]),t._v(" 项目结构")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ xxx tree\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v("\n├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go\n├── config\n│ ├── default.properties\n│ ├── dev.properties\n│ ├── local.properties\n│ └── prod.properties\n├── docker-compose.yaml\n├── go.mod\n├── internal\n│ ├── controller\n│ │ └── demo_ctr.go\n│ ├── interface\n│ │ ├── domain\n│ │ │ ├── demo.go\n│ │ │ └── user.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v("\n│ │ └── i_demo.go\n│ ├── master.go\n│ ├── middleware\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module\n│ │ └── demo\n│ │ ├── demo_svc.go\n│ │ └── error.go\n│ ├── pkg\n│ │ └── utils\n│ │ └── error.go\n│ └── router\n│ ├── auth_router.go\n│ └── pub_router.go\n└── tests\n └── api\n └── demo.http\n\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("17")]),t._v(" directories, "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("23")]),t._v(" files\n")])])]),s("ul",[s("li",[t._v("cmd/server/main.go: 启动文件,main函数所在文件")]),t._v(" "),s("li",[t._v("config/: 项目配置文件目录,支持"),s("code",[t._v(".properties")]),t._v("文件")]),t._v(" "),s("li",[t._v("internal/router/: 在该目录定义路由器")]),t._v(" "),s("li",[t._v("internal/middleware/: 中间件目录,如果需要定义web中间件,在该目录编写")]),t._v(" "),s("li",[t._v("internal/controller/: controller目录,在该目录中的文件定义路由")]),t._v(" "),s("li",[t._v("internal/interface/service/: 该目录放服务的接口定义")]),t._v(" "),s("li",[t._v("internal/domain/: 该目录放领域对象")]),t._v(" "),s("li",[t._v("internal/entity/: 该目录放一些无逻辑的结构体,类似于Java 的POJO")]),t._v(" "),s("li",[t._v("internal/module/: 模块目录,下面的每一个子目录实现一个模块的功能,一般是internal/interface/service/中定义的服务的业务实现;")]),t._v(" "),s("li",[t._v("internal/pkg/: 在该目录可以放一些项目共用的工具代码")]),t._v(" "),s("li",[t._v("internal/master.go: 存放"),s("strong",[t._v("MasterPriest")]),t._v("函数")]),t._v(" "),s("li",[t._v("internal/priest.go: gone priest 命令生成的 "),s("strong",[t._v("Priest")]),t._v("函数,用于"),s("strong",[t._v("埋葬")]),t._v("所有Goner")])]),t._v(" "),s("h2",{attrs:{id:"router"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#router"}},[t._v("#")]),t._v(" Router")]),t._v(" "),s("p",[t._v("在目录"),s("code",[t._v("internal/router")]),t._v("中分别实现了两个"),s("code",[t._v("gin.IRouter")]),t._v(":")]),t._v(" "),s("ul",[s("li",[t._v("pubRouter,公开的路由,挂载在该路由下的接口,请求将无需授权即可访问。")]),t._v(" "),s("li",[t._v("authRouter,鉴权的路由,挂载在该路由下的接口,请求必须先要经过授权。")])]),t._v(" "),s("p",[t._v("我们来分析"),s("code",[t._v("internal/router/pub_router.go")]),t._v("的代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" router\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/middleware"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" IdRouterPub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"router-pub"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPubRouter")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" IdRouterPub\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" pubRouter "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter\n\troot gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v("\n\tpub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("middleware"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PubMiddleware "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("root"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/api"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pub"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Next"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ol",[s("li",[t._v("对于router,需要实现了"),s("code",[t._v("gin.IRouter")]),t._v("接口中定义的方法;")]),t._v(" "),s("li",[t._v("结构体"),s("code",[t._v("pubRouter")]),t._v("内嵌了一个"),s("code",[t._v("gin.IRouter")]),t._v(",就等于直接实现了"),s("code",[t._v("gin.IRouter")]),t._v("接口,只在"),s("code",[t._v("AfterRevive()")]),t._v("中将其赋予一个值就可以了;")]),t._v(" "),s("li",[s("code",[t._v('r.IRouter = r.root.Group("/api", r.pub.Next)')]),t._v("意思是当前路由是根路由下"),s("code",[t._v("/api")]),t._v("的子路由,并且默认的增加了一个中间"),s("code",[t._v("r.pub.Next")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v("root gin.IRouter")]),t._v("是一个Gone框架中"),s("code",[t._v("github.com/gone-io/gone/goner/gin")]),t._v("包提供的一个框架级Goner,具名注入的"),s("code",[t._v("gone-gin-router")]),t._v("。")])]),t._v(" "),s("h2",{attrs:{id:"controller"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#controller"}},[t._v("#")]),t._v(" Controller")]),t._v(" "),s("p",[t._v("下面是Controller接口的定义:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Controller 控制器接口,由业务代码编码实现,用于挂载和处理路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用方式参考 [示例代码](https://gitlab.openviewtech.com/gone/gone-example/-/tree/master/gone-app)")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Mount 路由挂载接口,改接口会在服务启动前被调用,该函数的实现通常情况应该返回`nil`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" MountError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("编写http接口,我们需要实现"),s("code",[t._v("Controller")]),t._v("接口,在"),s("code",[t._v("Mount")]),t._v("方法中实现接口路由的挂载,如"),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v("的代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" controller\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/service"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/pkg/utils"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入依赖的服务")]),t._v("\n\n\tauthRouter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-auth"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路由器")]),t._v("\n\tpubRouter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-pub"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路由器")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("MountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要鉴权的路由分组")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tauthRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不需要鉴权的路由分组")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/error"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/echo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("echo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("showDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tEcho "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"echo"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" req Req\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" ctx"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bind")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" utils"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ParameterParseError"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Echo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#service"}},[t._v("#")]),t._v(" Service")]),t._v(" "),s("p",[t._v("规范上,我们要求将服务的接口定义在"),s("code",[t._v("internal/interface/service")]),t._v("目录,文件名以"),s("code",[t._v("i_")]),t._v("打头,接口类型以"),s("code",[t._v("I")]),t._v("打头,例如:\n文件:i_demo.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" service\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IDemo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("DemoEntity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("服务的逻辑实现,放到"),s("code",[t._v("internal/module")]),t._v("目录分模块实现。")]),t._v(" "),s("h2",{attrs:{id:"数据库操作"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#数据库操作"}},[t._v("#")]),t._v(" 数据库操作")]),t._v(" "),s("p",[t._v("在下一个例子中,我们将构建使用MySQL作为数据的Web项目。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[68],{355:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"web-项目"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#web-项目"}},[t._v("#")]),t._v(" Web 项目")]),t._v(" "),s("h2",{attrs:{id:"安装gone辅助工具"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#安装gone辅助工具"}},[t._v("#")]),t._v(" 安装gone辅助工具")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("go "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("install")]),t._v(" github.com/gone-io/gone/tools/gone@latest\n")])])]),s("blockquote",[s("p",[t._v("安装可以使用gone命令:")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("gone "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n")])])]),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("\t➜ demo gone "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v("\n\tNAME:\n\tgone - A new cli application\n\n\tUSAGE:\n\tgone "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("global options"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("command options"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("arguments"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("..")]),t._v("."),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\n\tDESCRIPTION:\n\tgenerate gone code or generate gone app\n\n\tCOMMANDS:\n\tpriest "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-s")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${scanPackageDir}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-p")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${pkgName}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${funcName}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outputFilePath}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-w"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("\n\tmock "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-f")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${fromGoFile}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-o")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${outGoFile}")]),t._v("\n\tcreate "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-t "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${template}")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),t._v("-m "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${modName}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token variable"}},[t._v("${appName}")]),t._v("\n\thelp, h Shows a list of commands or "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("for")]),t._v(" one "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("command")]),t._v("\n\n\tGLOBAL OPTIONS:\n\t--help, "),s("span",{pre:!0,attrs:{class:"token parameter variable"}},[t._v("-h")]),t._v(" show "),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("help")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("default: "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("false")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("支持的功能:")]),t._v(" "),s("ol",[s("li",[t._v("create,创建一个gone app,暂时只支持创建web app")]),t._v(" "),s("li",[t._v("priest,为项目自动生成 "),s("strong",[t._v("Priest")]),t._v(" 函数,"),s("RouterLink",{attrs:{to:"/zh/"}},[t._v("了解更多")])],1),t._v(" "),s("li",[t._v("生成用于测试的mock代码")])])]),t._v(" "),s("h2",{attrs:{id:"创建一个web项目并运行代码"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#创建一个web项目并运行代码"}},[t._v("#")]),t._v(" 创建一个web项目并运行代码")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("# 创将一个名为 web-app 的项目")]),t._v("\ngone create web-app\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v("cd")]),t._v(" web-app\n"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("make")]),t._v(" run\n")])])]),s("h2",{attrs:{id:"项目结构"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#项目结构"}},[t._v("#")]),t._v(" 项目结构")]),t._v(" "),s("div",{staticClass:"language-bash extra-class"},[s("pre",{pre:!0,attrs:{class:"language-bash"}},[s("code",[t._v("➜ xxx tree\n"),s("span",{pre:!0,attrs:{class:"token builtin class-name"}},[t._v(".")]),t._v("\n├── Dockerfile\n├── Makefile\n├── README.md\n├── cmd\n│ └── server\n│ └── main.go\n├── config\n│ ├── default.properties\n│ ├── dev.properties\n│ ├── local.properties\n│ └── prod.properties\n├── docker-compose.yaml\n├── go.mod\n├── internal\n│ ├── controller\n│ │ └── demo_ctr.go\n│ ├── interface\n│ │ ├── domain\n│ │ │ ├── demo.go\n│ │ │ └── user.go\n│ │ └── "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("service")]),t._v("\n│ │ └── i_demo.go\n│ ├── master.go\n│ ├── middleware\n│ │ ├── authorize.go\n│ │ └── pub.go\n│ ├── module\n│ │ └── demo\n│ │ ├── demo_svc.go\n│ │ └── error.go\n│ ├── pkg\n│ │ └── utils\n│ │ └── error.go\n│ └── router\n│ ├── auth_router.go\n│ └── pub_router.go\n└── tests\n └── api\n └── demo.http\n\n"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("17")]),t._v(" directories, "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("23")]),t._v(" files\n")])])]),s("ul",[s("li",[t._v("cmd/server/main.go: 启动文件,main函数所在文件")]),t._v(" "),s("li",[t._v("config/: 项目配置文件目录,支持"),s("code",[t._v(".properties")]),t._v("文件")]),t._v(" "),s("li",[t._v("internal/router/: 在该目录定义路由器")]),t._v(" "),s("li",[t._v("internal/middleware/: 中间件目录,如果需要定义web中间件,在该目录编写")]),t._v(" "),s("li",[t._v("internal/controller/: controller目录,在该目录中的文件定义路由")]),t._v(" "),s("li",[t._v("internal/interface/service/: 该目录放服务的接口定义")]),t._v(" "),s("li",[t._v("internal/domain/: 该目录放领域对象")]),t._v(" "),s("li",[t._v("internal/entity/: 该目录放一些无逻辑的结构体,类似于Java 的POJO")]),t._v(" "),s("li",[t._v("internal/module/: 模块目录,下面的每一个子目录实现一个模块的功能,一般是internal/interface/service/中定义的服务的业务实现;")]),t._v(" "),s("li",[t._v("internal/pkg/: 在该目录可以放一些项目共用的工具代码")]),t._v(" "),s("li",[t._v("internal/master.go: 存放"),s("strong",[t._v("MasterPriest")]),t._v("函数")]),t._v(" "),s("li",[t._v("internal/priest.go: gone priest 命令生成的 "),s("strong",[t._v("Priest")]),t._v("函数,用于"),s("strong",[t._v("埋葬")]),t._v("所有Goner")])]),t._v(" "),s("h2",{attrs:{id:"router"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#router"}},[t._v("#")]),t._v(" Router")]),t._v(" "),s("p",[t._v("在目录"),s("code",[t._v("internal/router")]),t._v("中分别实现了两个"),s("code",[t._v("gin.IRouter")]),t._v(":")]),t._v(" "),s("ul",[s("li",[t._v("pubRouter,公开的路由,挂载在该路由下的接口,请求将无需授权即可访问。")]),t._v(" "),s("li",[t._v("authRouter,鉴权的路由,挂载在该路由下的接口,请求必须先要经过授权。")])]),t._v(" "),s("p",[t._v("我们来分析"),s("code",[t._v("internal/router/pub_router.go")]),t._v("的代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" router\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/middleware"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("const")]),t._v(" IdRouterPub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"router-pub"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewPubRouter")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("GonerId"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" IdRouterPub\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" pubRouter "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter\n\troot gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-gin-router"`')]),t._v("\n\tpub "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("middleware"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("PubMiddleware "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("pubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("AfterRevive")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("AfterReviveError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("root"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/api"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pub"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Next"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("ol",[s("li",[t._v("对于router,需要实现了"),s("code",[t._v("gin.IRouter")]),t._v("接口中定义的方法;")]),t._v(" "),s("li",[t._v("结构体"),s("code",[t._v("pubRouter")]),t._v("内嵌了一个"),s("code",[t._v("gin.IRouter")]),t._v(",就等于直接实现了"),s("code",[t._v("gin.IRouter")]),t._v("接口,只在"),s("code",[t._v("AfterRevive()")]),t._v("中将其赋予一个值就可以了;")]),t._v(" "),s("li",[s("code",[t._v('r.IRouter = r.root.Group("/api", r.pub.Next)')]),t._v("意思是当前路由是根路由下"),s("code",[t._v("/api")]),t._v("的子路由,并且默认的增加了一个中间"),s("code",[t._v("r.pub.Next")]),t._v(";")]),t._v(" "),s("li",[s("code",[t._v("root gin.IRouter")]),t._v("是一个Gone框架中"),s("code",[t._v("github.com/gone-io/gone/goner/gin")]),t._v("包提供的一个框架级Goner,具名注入的"),s("code",[t._v("gone-gin-router")]),t._v("。")])]),t._v(" "),s("h2",{attrs:{id:"controller"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#controller"}},[t._v("#")]),t._v(" Controller")]),t._v(" "),s("p",[t._v("下面是Controller接口的定义:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Controller 控制器接口,由业务代码编码实现,用于挂载和处理路由")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用方式参考 [示例代码](https://gitlab.openviewtech.com/gone/gone-example/-/tree/master/gone-app)")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Controller "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Mount 路由挂载接口,改接口会在服务启动前被调用,该函数的实现通常情况应该返回`nil`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" MountError\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("编写http接口,我们需要实现"),s("code",[t._v("Controller")]),t._v("接口,在"),s("code",[t._v("Mount")]),t._v("方法中实现接口路由的挂载,如"),s("code",[t._v("internal/controller/demo_ctr.go")]),t._v("的代码:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" controller\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/service"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/pkg/utils"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone"')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/gin"')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//go:gone")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewDemoController")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Goner "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" demoController "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tdemoSvc service"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IDemo "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"*"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入依赖的服务")]),t._v("\n\n\tauthRouter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-auth"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路由器")]),t._v("\n\tpubRouter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("IRouter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"router-pub"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入路由器")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Mount")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("MountError "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//需要鉴权的路由分组")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tauthRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不需要鉴权的路由分组")]),t._v("\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\tpubRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo2"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/show"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("showDemo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/error"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/echo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("echo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("showDemo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("demoController"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("ctx "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Context"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tEcho "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"echo"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" req Req\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" ctx"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Bind")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(";")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("NewParameterError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" utils"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ParameterParseError"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" ctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("demoSvc"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Echo"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"service"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#service"}},[t._v("#")]),t._v(" Service")]),t._v(" "),s("p",[t._v("规范上,我们要求将服务的接口定义在"),s("code",[t._v("internal/interface/service")]),t._v("目录,文件名以"),s("code",[t._v("i_")]),t._v("打头,接口类型以"),s("code",[t._v("I")]),t._v("打头,例如:\n文件:i_demo.go")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("package")]),t._v(" service\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"web-app/internal/interface/domain"')]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" IDemo "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Show")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("domain"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("DemoEntity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Echo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("input "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("服务的逻辑实现,放到"),s("code",[t._v("internal/module")]),t._v("目录分模块实现。")]),t._v(" "),s("h2",{attrs:{id:"数据库操作"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#数据库操作"}},[t._v("#")]),t._v(" 数据库操作")]),t._v(" "),s("p",[t._v("在下一个例子中,我们将构建使用MySQL作为数据的Web项目。")])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/69.25d30512.js b/assets/js/69.83f91289.js similarity index 95% rename from assets/js/69.25d30512.js rename to assets/js/69.83f91289.js index e8a2ebd..04306e0 100644 --- a/assets/js/69.25d30512.js +++ b/assets/js/69.83f91289.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[69],{358:function(t,e,r){"use strict";r.r(e);var s=r(10),a=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"参考文档"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#参考文档"}},[t._v("#")]),t._v(" 参考文档")]),t._v(" "),e("h2",{attrs:{id:"gone-辅助工具"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone-辅助工具"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/gone-tool.html"}},[t._v("gone 辅助工具")])],1),t._v(" "),e("h2",{attrs:{id:"http-注入说明"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-注入说明"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/http-inject.html"}},[t._v("HTTP 注入说明")])],1),t._v(" "),e("h2",{attrs:{id:"gone对xorm的增强"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone对xorm的增强"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/xorm.html"}},[t._v("Gone对Xorm的增强")])],1),t._v(" "),e("h2",{attrs:{id:"redis-参考文档"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#redis-参考文档"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/redis.html"}},[t._v("Redis 参考文档")])],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[69],{364:function(t,e,r){"use strict";r.r(e);var s=r(10),a=Object(s.a)({},(function(){var t=this,e=t._self._c;return e("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[e("h1",{attrs:{id:"参考文档"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#参考文档"}},[t._v("#")]),t._v(" 参考文档")]),t._v(" "),e("h2",{attrs:{id:"gone-辅助工具"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone-辅助工具"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/gone-tool.html"}},[t._v("gone 辅助工具")])],1),t._v(" "),e("h2",{attrs:{id:"http-注入说明"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#http-注入说明"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/http-inject.html"}},[t._v("HTTP 注入说明")])],1),t._v(" "),e("h2",{attrs:{id:"gone对xorm的增强"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#gone对xorm的增强"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/xorm.html"}},[t._v("Gone对Xorm的增强")])],1),t._v(" "),e("h2",{attrs:{id:"redis-参考文档"}},[e("a",{staticClass:"header-anchor",attrs:{href:"#redis-参考文档"}},[t._v("#")]),t._v(" "),e("RouterLink",{attrs:{to:"/zh/references/redis.html"}},[t._v("Redis 参考文档")])],1)])}),[],!1,null,null,null);e.default=a.exports}}]); \ No newline at end of file diff --git a/assets/js/71.fc3b9d8e.js b/assets/js/71.125c5fc8.js similarity index 99% rename from assets/js/71.fc3b9d8e.js rename to assets/js/71.125c5fc8.js index bdaac4c..fd04e67 100644 --- a/assets/js/71.fc3b9d8e.js +++ b/assets/js/71.125c5fc8.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[71],{364:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"http-注入说明"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-注入说明"}},[t._v("#")]),t._v(" HTTP 注入说明")]),t._v(" "),s("h2",{attrs:{id:"http-依赖注入标签的格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-依赖注入标签的格式"}},[t._v("#")]),t._v(" HTTP 依赖注入标签的格式")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v('${attributeName} ${attributeType} gone:"http,${kind}=${key}"\n')])])]),s("p",[t._v("举例:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("router"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("function")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入值in.selects为`[]int{1,2,3}`")]),t._v("\n fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("上面例子中,")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("selects")]),t._v("为属性名(attributeName);")]),t._v(" "),s("li",[s("code",[t._v("[]int")]),t._v("为属性类型(attributeType);")]),t._v(" "),s("li",[s("code",[t._v("query")]),t._v("为注入类型(kind);")]),t._v(" "),s("li",[s("code",[t._v("select")]),t._v("为注入键值(key)。")])]),t._v(" "),s("h2",{attrs:{id:"支持注入的类型和响应标签"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的类型和响应标签"}},[t._v("#")]),t._v(" 支持注入的类型和响应标签")]),t._v(" "),s("table",[s("thead",[s("tr",[s("th",[t._v("名称")]),t._v(" "),s("th",[t._v("属性类型"),s("code",[t._v("${attributeType}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"center"}},[t._v("注入类型"),s("code",[t._v("${kind}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"center"}},[t._v("注入键值"),s("code",[t._v("${key}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"left"}},[t._v("说明")])])]),t._v(" "),s("tbody",[s("tr",[s("td",[s("strong",[t._v("上下文注入")])]),t._v(" "),s("td",[s("code",[t._v("gone.Context")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(不推荐)注入gin请求上下文对象,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("上下文注入")])]),t._v(" "),s("td",[s("code",[t._v("*gone.Context")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入gin请求上下文指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求注入")])]),t._v(" "),s("td",[s("code",[t._v("http.Request")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("不推荐)注入http.Request对象,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求注入")])]),t._v(" "),s("td",[s("code",[t._v("*http.Request")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入http.Request指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("地址注入")])]),t._v(" "),s("td",[s("code",[t._v("url.URL")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(不推荐)注入url.URL,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("地址注入")])]),t._v(" "),s("td",[s("code",[t._v("*url.URL")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入url.URL指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求头注入")])]),t._v(" "),s("td",[s("code",[t._v("http.Header")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入http.Header(请求头),不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("响应注入")])]),t._v(" "),s("td",[s("code",[t._v("gone.ResponseWriter")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("注入gin.ResponseWriter(用于直接写入响应数据),不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Body注入")])]),t._v(" "),s("td",[t._v("结构体、结构体指针")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[s("code",[t._v("body")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[s("strong",[t._v("body注入")]),t._v(";将请求body解析后注入到属性,注入类型为 "),s("code",[t._v("body")]),t._v(",不需要“注入键值"),s("code",[t._v("${key}")]),t._v("”;框架根据"),s("code",[t._v("Content-Type")]),t._v("自动判定是json还是xml等格式;每个请求处理函数只允许存在一个"),s("strong",[t._v("body注入")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求头单值注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("header")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以键值"),s("code",[t._v("${key}")]),t._v("为"),s("code",[t._v("key")]),t._v("获取请求头,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("URL路径参数注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("param")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Param(key)")]),t._v("获取Url中定义的参数值,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Query参数注入")])]),t._v(" "),s("td",[t._v("number | string | []number | []string | 结构体 | 结构体指针")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("query")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Query(key)")]),t._v("获取Query中的参数,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(","),s("strong",[t._v("支持简单类型的数组")]),t._v(",支持结构体和结构体指针,解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Cookie注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("cookie")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Context.Cookie(key)")]),t._v("获取Cookie的值,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])])])]),t._v(" "),s("h2",{attrs:{id:"query参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#query参数注入"}},[t._v("#")]),t._v(" Query参数注入")]),t._v(" "),s("h3",{attrs:{id:"属性类型为简单类型-1"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为简单类型-1"}},[t._v("#")]),t._v(" 属性类型为简单类型"),s("sub",[t._v("[1]")])]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tyourName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=name"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册请求query中的name参数")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册请求query中的name参数;不指定参数名,则取属性名作为参数名")]),t._v("\n age "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=age"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//int类型")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, %s, your name is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("yourName"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"属性类型为简单类型的数组"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为简单类型的数组"}},[t._v("#")]),t._v(" 属性类型为简单类型的数组")]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。\n下面代码,query为"),s("code",[t._v("?keyword=gone&keyword=is&keyword=best")]),t._v(","),s("code",[t._v("in.keywords")]),t._v("的值将会为 "),s("code",[t._v('[]string{"gone","is","best"}')]),t._v("。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tkeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keyword"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//简单类型的query数组参数注入")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"属性类型为结构体或者结构体指针"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为结构体或者结构体指针"}},[t._v("#")]),t._v(" 属性类型为结构体或者结构体指针")]),t._v(" "),s("p",[t._v("这种类型key无需指定;假设query为"),s("code",[t._v("?page=1&pageSize=20&keyword=gone&keyword=is&keyword=best")]),t._v(","),s("code",[t._v("in.req")]),t._v("的值将会为 "),s("code",[t._v('{1,20,[]string{"gone","is","best"}}')]),t._v(";注意结构体中可以使用"),s("code",[t._v("form")]),t._v("标签进行属性映射。")]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tPage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page"`')]),t._v("\n\t\tPageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize"`')]),t._v("\n\t\tKeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"keywords"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v("\n req2 "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"url路径参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#url路径参数注入"}},[t._v("#")]),t._v(" URL路径参数注入")]),t._v(" "),s("p",[t._v("URL 路径参数,是指定义在URL路由中的参数,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//url中定义参数名为name")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\tname2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param=name"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"body注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#body注入"}},[t._v("#")]),t._v(" Body注入")]),t._v(" "),s("p",[t._v("Body注入,是指读取HTTP请求正文内容,解析成结构体,注入属性的类型支持结构体、结构体指针,解析不了会返回参数错误。")]),t._v(" "),s("p",[t._v("支持多种ContentType:json、xml、form-data、form-urlencoded等,不传ContentType时,默认为application/x-www-form-urlencoded。")]),t._v(" "),s("p",[t._v("结构体可以使用"),s("code",[t._v("form")]),t._v("标签进行form-data、form-urlencoded的属性映射;"),s("code",[t._v("xml")]),t._v("标签进行xml的属性映射;"),s("code",[t._v("json")]),t._v("标签进行json的属性映射。")]),t._v(" "),s("p",[t._v("具体规则可以参考:"),s("a",{attrs:{href:"https://github.com/gin-gonic/gin/blob/master/docs/doc.md#model-binding-and-validation",target:"_blank",rel:"noopener noreferrer"}},[t._v("gin#Model binding and validation"),s("OutboundLink")],1),t._v("。")]),t._v(" "),s("p",[t._v("举例如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tPage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page" json:"page,omitempty" xml:"page" binding:"required"`')]),t._v("\n\t\tPageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize" json:"pageSize,omitempty" xml:"pageSize" binding:"required"`')]),t._v("\n\t\tKeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"keywords" json:"keywords,omitempty" xml:"keywords" binding:"required"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:body只能被注入一次,因为 writer被读取后就变成空了")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// req2 *Req `gone:"http,body"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"请求头注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求头注入"}},[t._v("#")]),t._v(" 请求头注入")]),t._v(" "),s("p",[t._v("HTTP请求头注入,用于获取某个请求头信息,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。\n比如下面代码,可以用于读取请求头中的"),s("code",[t._v("Content-Type")]),t._v("信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tcontentType "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\tcontentType2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header=contentType"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, contentType = "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("contentType\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"cookie注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#cookie注入"}},[t._v("#")]),t._v(" Cookie注入")]),t._v(" "),s("p",[t._v("Cookie注入,用于获取某个cookie信息,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\ttoken "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,cookie"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\ttoken2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header=token"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"高级"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#高级"}},[t._v("#")]),t._v(" 高级")]),t._v(" "),s("p",[t._v("另外,我们还支持几种特殊结构体(或结构体指针、接口、map)的注入,由于golang的“值拷贝”推荐使用指针注入,这些结构体代表了HTTP请求、响应、上下文等,这些结构体的注入不需要指定"),s("code",[t._v("kind")]),t._v("和"),s("code",[t._v("key")]),t._v("。")]),t._v(" "),s("h3",{attrs:{id:"url结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#url结构体注入"}},[t._v("#")]),t._v(" URL结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("url.URL")]),t._v(" 或者 "),s("code",[t._v("*url.URL")]),t._v(",该类型定义在"),s("code",[t._v("net/url")]),t._v("包中,代表了HTTP请求的URL。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\turl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("url"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("URL "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\turl2 url"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("URL "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求头注入-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求头注入-2"}},[t._v("#")]),t._v(" 请求头注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("http.Header")]),t._v(",该类型定义在"),s("code",[t._v("net/http")]),t._v("包中,代表了HTTP请求的Header。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\theader http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Header "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"上下文结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#上下文结构体注入"}},[t._v("#")]),t._v(" 上下文结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("gin.Content")]),t._v(" 或者 "),s("code",[t._v("*gin.Content")]),t._v(",该类型定义在"),s("code",[t._v("github.com/gin-gonic/gin")]),t._v("包中,代表了HTTP请求的上下文。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tcontext "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\tcontext2 gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求结构体注入"}},[t._v("#")]),t._v(" 请求结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("http.Request")]),t._v(" 或者 "),s("code",[t._v("*http.Request")]),t._v(",该类型定义在"),s("code",[t._v("net/http")]),t._v("包中,代表了HTTP请求信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\trequest "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Request "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\trequest2 http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Request "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求响应接口注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求响应接口注入"}},[t._v("#")]),t._v(" 请求响应接口注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("gin.ResponseWriter")]),t._v(",该类型定义在"),s("code",[t._v("github.com/gin-gonic/gin")]),t._v("包中,代表了HTTP响应信息,可以使用该接口响应请求信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\twriter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ResponseWriter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"备注"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#备注"}},[t._v("#")]),t._v(" 备注")]),t._v(" "),s("p",[t._v("[1]. 简单类型指 字符串、布尔类型 和 数字类型,其中数字类型包括:")]),t._v(" "),s("ul",[s("li",[t._v("整数类型:int、uint、int8、uint8、int16、uint16、int32、uint32、int64、uint64")]),t._v(" "),s("li",[t._v("非负整数类型:uint、uint8、uint16、uint32、uint64")]),t._v(" "),s("li",[t._v("浮点类型:float32、float64")])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[71],{362:function(t,s,n){"use strict";n.r(s);var a=n(10),e=Object(a.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"http-注入说明"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-注入说明"}},[t._v("#")]),t._v(" HTTP 注入说明")]),t._v(" "),s("h2",{attrs:{id:"http-依赖注入标签的格式"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#http-依赖注入标签的格式"}},[t._v("#")]),t._v(" HTTP 依赖注入标签的格式")]),t._v(" "),s("div",{staticClass:"language- extra-class"},[s("pre",{pre:!0,attrs:{class:"language-text"}},[s("code",[t._v('${attributeName} ${attributeType} gone:"http,${kind}=${key}"\n')])])]),s("p",[t._v("举例:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("router"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("GET")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/search"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("function")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n selects "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=select"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入值in.selects为`[]int{1,2,3}`")]),t._v("\n fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("selects"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("上面例子中,")]),t._v(" "),s("ul",[s("li",[s("code",[t._v("selects")]),t._v("为属性名(attributeName);")]),t._v(" "),s("li",[s("code",[t._v("[]int")]),t._v("为属性类型(attributeType);")]),t._v(" "),s("li",[s("code",[t._v("query")]),t._v("为注入类型(kind);")]),t._v(" "),s("li",[s("code",[t._v("select")]),t._v("为注入键值(key)。")])]),t._v(" "),s("h2",{attrs:{id:"支持注入的类型和响应标签"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#支持注入的类型和响应标签"}},[t._v("#")]),t._v(" 支持注入的类型和响应标签")]),t._v(" "),s("table",[s("thead",[s("tr",[s("th",[t._v("名称")]),t._v(" "),s("th",[t._v("属性类型"),s("code",[t._v("${attributeType}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"center"}},[t._v("注入类型"),s("code",[t._v("${kind}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"center"}},[t._v("注入键值"),s("code",[t._v("${key}")])]),t._v(" "),s("th",{staticStyle:{"text-align":"left"}},[t._v("说明")])])]),t._v(" "),s("tbody",[s("tr",[s("td",[s("strong",[t._v("上下文注入")])]),t._v(" "),s("td",[s("code",[t._v("gone.Context")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(不推荐)注入gin请求上下文对象,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("上下文注入")])]),t._v(" "),s("td",[s("code",[t._v("*gone.Context")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入gin请求上下文指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求注入")])]),t._v(" "),s("td",[s("code",[t._v("http.Request")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("不推荐)注入http.Request对象,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求注入")])]),t._v(" "),s("td",[s("code",[t._v("*http.Request")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入http.Request指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("地址注入")])]),t._v(" "),s("td",[s("code",[t._v("url.URL")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(不推荐)注入url.URL,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("地址注入")])]),t._v(" "),s("td",[s("code",[t._v("*url.URL")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入url.URL指针,不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求头注入")])]),t._v(" "),s("td",[s("code",[t._v("http.Header")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("(推荐)注入http.Header(请求头),不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("响应注入")])]),t._v(" "),s("td",[s("code",[t._v("gone.ResponseWriter")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("注入gin.ResponseWriter(用于直接写入响应数据),不需要类型"),s("code",[t._v("${kind}")]),t._v("和键值"),s("code",[t._v("${key}")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Body注入")])]),t._v(" "),s("td",[t._v("结构体、结构体指针")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[s("code",[t._v("body")])]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("/")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[s("strong",[t._v("body注入")]),t._v(";将请求body解析后注入到属性,注入类型为 "),s("code",[t._v("body")]),t._v(",不需要“注入键值"),s("code",[t._v("${key}")]),t._v("”;框架根据"),s("code",[t._v("Content-Type")]),t._v("自动判定是json还是xml等格式;每个请求处理函数只允许存在一个"),s("strong",[t._v("body注入")]),t._v("。")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("请求头单值注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("header")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以键值"),s("code",[t._v("${key}")]),t._v("为"),s("code",[t._v("key")]),t._v("获取请求头,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("URL路径参数注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("param")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Param(key)")]),t._v("获取Url中定义的参数值,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Query参数注入")])]),t._v(" "),s("td",[t._v("number | string | []number | []string | 结构体 | 结构体指针")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("query")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Query(key)")]),t._v("获取Query中的参数,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(","),s("strong",[t._v("支持简单类型的数组")]),t._v(",支持结构体和结构体指针,解析不了会返回参数错误")])]),t._v(" "),s("tr",[s("td",[s("strong",[t._v("Cookie注入")])]),t._v(" "),s("td",[t._v("number | string")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("cookie")]),t._v(" "),s("td",{staticStyle:{"text-align":"center"}},[t._v("缺省取字段名")]),t._v(" "),s("td",{staticStyle:{"text-align":"left"}},[t._v("以“注入键值"),s("code",[t._v("${key}")]),t._v("”为"),s("code",[t._v("key")]),t._v("调用函数"),s("code",[t._v("ctx.Context.Cookie(key)")]),t._v("获取Cookie的值,属性类型支持 简单类型"),s("sub",[t._v("[1]")]),t._v(",解析不了会返回参数错误")])])])]),t._v(" "),s("h2",{attrs:{id:"query参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#query参数注入"}},[t._v("#")]),t._v(" Query参数注入")]),t._v(" "),s("h3",{attrs:{id:"属性类型为简单类型-1"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为简单类型-1"}},[t._v("#")]),t._v(" 属性类型为简单类型"),s("sub",[t._v("[1]")])]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tyourName "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=name"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册请求query中的name参数")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注册请求query中的name参数;不指定参数名,则取属性名作为参数名")]),t._v("\n age "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=age"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//int类型")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, %s, your name is %s"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("yourName"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"属性类型为简单类型的数组"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为简单类型的数组"}},[t._v("#")]),t._v(" 属性类型为简单类型的数组")]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。\n下面代码,query为"),s("code",[t._v("?keyword=gone&keyword=is&keyword=best")]),t._v(","),s("code",[t._v("in.keywords")]),t._v("的值将会为 "),s("code",[t._v('[]string{"gone","is","best"}')]),t._v("。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tkeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query=keyword"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//简单类型的query数组参数注入")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"属性类型为结构体或者结构体指针"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#属性类型为结构体或者结构体指针"}},[t._v("#")]),t._v(" 属性类型为结构体或者结构体指针")]),t._v(" "),s("p",[t._v("这种类型key无需指定;假设query为"),s("code",[t._v("?page=1&pageSize=20&keyword=gone&keyword=is&keyword=best")]),t._v(","),s("code",[t._v("in.req")]),t._v("的值将会为 "),s("code",[t._v('{1,20,[]string{"gone","is","best"}}')]),t._v(";注意结构体中可以使用"),s("code",[t._v("form")]),t._v("标签进行属性映射。")]),t._v(" "),s("p",[t._v("解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tPage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page"`')]),t._v("\n\t\tPageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize"`')]),t._v("\n\t\tKeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"keywords"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v("\n req2 "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,query"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"url路径参数注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#url路径参数注入"}},[t._v("#")]),t._v(" URL路径参数注入")]),t._v(" "),s("p",[t._v("URL 路径参数,是指定义在URL路由中的参数,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello/:name"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//url中定义参数名为name")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tname "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\tname2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,param=name"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("name\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"body注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#body注入"}},[t._v("#")]),t._v(" Body注入")]),t._v(" "),s("p",[t._v("Body注入,是指读取HTTP请求正文内容,解析成结构体,注入属性的类型支持结构体、结构体指针,解析不了会返回参数错误。")]),t._v(" "),s("p",[t._v("支持多种ContentType:json、xml、form-data、form-urlencoded等,不传ContentType时,默认为application/x-www-form-urlencoded。")]),t._v(" "),s("p",[t._v("结构体可以使用"),s("code",[t._v("form")]),t._v("标签进行form-data、form-urlencoded的属性映射;"),s("code",[t._v("xml")]),t._v("标签进行xml的属性映射;"),s("code",[t._v("json")]),t._v("标签进行json的属性映射。")]),t._v(" "),s("p",[t._v("具体规则可以参考:"),s("a",{attrs:{href:"https://github.com/gin-gonic/gin/blob/master/docs/doc.md#model-binding-and-validation",target:"_blank",rel:"noopener noreferrer"}},[t._v("gin#Model binding and validation"),s("OutboundLink")],1),t._v("。")]),t._v(" "),s("p",[t._v("举例如下:")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Req "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tPage "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"page" json:"page,omitempty" xml:"page" binding:"required"`')]),t._v("\n\t\tPageSize "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"pageSize" json:"pageSize,omitempty" xml:"pageSize" binding:"required"`')]),t._v("\n\t\tKeywords "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`form:"keywords" json:"keywords,omitempty" xml:"keywords" binding:"required"`')]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\treq Req "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,body"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:body只能被注入一次,因为 writer被读取后就变成空了")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v('// req2 *Req `gone:"http,body"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" fmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Sprintf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, keywords is [%v]"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("req"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Keywords"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"请求头注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求头注入"}},[t._v("#")]),t._v(" 请求头注入")]),t._v(" "),s("p",[t._v("HTTP请求头注入,用于获取某个请求头信息,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。\n比如下面代码,可以用于读取请求头中的"),s("code",[t._v("Content-Type")]),t._v("信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tcontentType "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\tcontentType2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header=contentType"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, contentType = "')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("contentType\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"cookie注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#cookie注入"}},[t._v("#")]),t._v(" Cookie注入")]),t._v(" "),s("p",[t._v("Cookie注入,用于获取某个cookie信息,注入属性的类型支持"),s("code",[t._v("string")]),t._v("和"),s("code",[t._v("int")]),t._v(","),s("code",[t._v("uint")]),t._v(","),s("code",[t._v("float64")]),t._v("等数字类型,解析不了会返回参数错误。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\ttoken "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,cookie"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//不指定参数名,使用属性名作为参数名")]),t._v("\n\t\t\t\ttoken2 "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http,header=token"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用key指定参数名")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"高级"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#高级"}},[t._v("#")]),t._v(" 高级")]),t._v(" "),s("p",[t._v("另外,我们还支持几种特殊结构体(或结构体指针、接口、map)的注入,由于golang的“值拷贝”推荐使用指针注入,这些结构体代表了HTTP请求、响应、上下文等,这些结构体的注入不需要指定"),s("code",[t._v("kind")]),t._v("和"),s("code",[t._v("key")]),t._v("。")]),t._v(" "),s("h3",{attrs:{id:"url结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#url结构体注入"}},[t._v("#")]),t._v(" URL结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("url.URL")]),t._v(" 或者 "),s("code",[t._v("*url.URL")]),t._v(",该类型定义在"),s("code",[t._v("net/url")]),t._v("包中,代表了HTTP请求的URL。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\turl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("url"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("URL "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\turl2 url"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("URL "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求头注入-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求头注入-2"}},[t._v("#")]),t._v(" 请求头注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("http.Header")]),t._v(",该类型定义在"),s("code",[t._v("net/http")]),t._v("包中,代表了HTTP请求的Header。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\theader http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Header "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"上下文结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#上下文结构体注入"}},[t._v("#")]),t._v(" 上下文结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("gin.Content")]),t._v(" 或者 "),s("code",[t._v("*gin.Content")]),t._v(",该类型定义在"),s("code",[t._v("github.com/gin-gonic/gin")]),t._v("包中,代表了HTTP请求的上下文。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\tcontext "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\tcontext2 gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Content "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求结构体注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求结构体注入"}},[t._v("#")]),t._v(" 请求结构体注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("http.Request")]),t._v(" 或者 "),s("code",[t._v("*http.Request")]),t._v(",该类型定义在"),s("code",[t._v("net/http")]),t._v("包中,代表了HTTP请求信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\trequest "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Request "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体指针")]),t._v("\n\t\t\t\trequest2 http"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Request "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用结构体")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h3",{attrs:{id:"请求响应接口注入"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#请求响应接口注入"}},[t._v("#")]),t._v(" 请求响应接口注入")]),t._v(" "),s("p",[t._v("支持属性类型为 "),s("code",[t._v("gin.ResponseWriter")]),t._v(",该类型定义在"),s("code",[t._v("github.com/gin-gonic/gin")]),t._v("包中,代表了HTTP响应信息,可以使用该接口响应请求信息。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tctr"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("rootRouter"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Group")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/demo"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("POST")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"/hello"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("in "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\twriter gin"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("ResponseWriter "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"http"`')]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"hello, your token in cookie is"')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("+")]),t._v(" in"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("token\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"备注"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#备注"}},[t._v("#")]),t._v(" 备注")]),t._v(" "),s("p",[t._v("[1]. 简单类型指 字符串、布尔类型 和 数字类型,其中数字类型包括:")]),t._v(" "),s("ul",[s("li",[t._v("整数类型:int、uint、int8、uint8、int16、uint16、int32、uint32、int64、uint64")]),t._v(" "),s("li",[t._v("非负整数类型:uint、uint8、uint16、uint32、uint64")]),t._v(" "),s("li",[t._v("浮点类型:float32、float64")])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/72.805cd014.js b/assets/js/72.9e8c3622.js similarity index 99% rename from assets/js/72.805cd014.js rename to assets/js/72.9e8c3622.js index b98be58..a9af85b 100644 --- a/assets/js/72.805cd014.js +++ b/assets/js/72.9e8c3622.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[72],{363:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"redis-参考文档"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#redis-参考文档"}},[t._v("#")]),t._v(" Redis 参考文档")]),t._v(" "),s("h2",{attrs:{id:"配置项"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置项"}},[t._v("#")]),t._v(" 配置项")]),t._v(" "),s("ul",[s("li",[t._v("redis.max-idle:最大空闲链接数,不配置默认为2")]),t._v(" "),s("li",[t._v("redis.max-active:最大活跃链接数,不配置默认为10")]),t._v(" "),s("li",[t._v("redis.db:使用的db,不配置默认为0")]),t._v(" "),s("li",[t._v("redis.cache.prefix:key前缀,如果设置了,对redis的增删改查都会拼接该前缀,拼接方式${prefix}#${key};默认为空")])]),t._v(" "),s("h2",{attrs:{id:"import-和-bury"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#import-和-bury"}},[t._v("#")]),t._v(" import 和 bury")]),t._v(" "),s("ul",[s("li",[t._v("import")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n")])])]),s("ul",[s("li",[t._v("bury")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用分布是缓存-redis-cache"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布是缓存-redis-cache"}},[t._v("#")]),t._v(" 使用分布是缓存 redis.Cache")]),t._v(" "),s("h3",{attrs:{id:"接口定义"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cache "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存,Set的别名")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//删除缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Remove")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//根据模式获取缓存的key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Keys")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pattern "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取当前Cache使用的前缀")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prefix")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用分布式锁-redis-locker"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布式锁-redis-locker"}},[t._v("#")]),t._v(" 使用分布式锁 redis.Locker")]),t._v(" "),s("h3",{attrs:{id:"接口定义-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-2"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Locker redis 分布式缓存")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Locker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TryLock 尝试将key锁定一段时间,成功返回一个解锁函数,失败返回错误")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("unlock Unlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//LockAndDo 尝试获取锁,并执行fn函数,函数未执行完自动续期,函数执行完后自动释放锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" fn "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" lockTime"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" checkPeriod time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-2"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"操作key-使用-redis-key"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#操作key-使用-redis-key"}},[t._v("#")]),t._v(" 操作Key,使用 redis.Key")]),t._v(" "),s("h3",{attrs:{id:"接口定义-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-3"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Key "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Expire 给key设置一段时间后过期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Expire")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//ExpireAt 设置key在某个时间点过期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ExpireAt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" time time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Ttl 获取key的过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Ttl")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Del 删除一个key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Del")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Incr 使key自增一个量")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Incr")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" increment "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Keys 根据模式获取缓存的key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Keys")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pattern "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Prefix 获取redis配置的key前缀")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prefix")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用-provider-注入-redis-接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用-provider-注入-redis-接口"}},[t._v("#")]),t._v(" 使用 Provider 注入 redis 接口")]),t._v(" "),s("p",[t._v("通过标签 "),s("code",[t._v('gone:"gone-redis-provider,${key}')]),t._v("使用redis Provider 创建一个接口给注入属性。支持注入的接口包括前面的"),s("code",[t._v("redis.Cache")]),t._v("、"),s("code",[t._v("redis.Locker")]),t._v("、"),s("code",[t._v("redis.Key")]),t._v(";另外还支持注入 "),s("code",[t._v("redis.Hash")]),t._v("接口,用于操作redis hash。")]),t._v(" "),s("p",[t._v("Hash接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Hash "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//删除hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Del")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//扫描hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Scan")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("byte")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//hash字段自增")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Incr")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" increment "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-3"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("ol",[s("li",[t._v("使用 Provider 创建一个redis.Cache接口,该缓存接口的前缀将使用配置中的前缀拼接${key}得到新的前缀,可以用于有按模块隔离的场景。\n如果配置了"),s("code",[t._v("redis.cache.prefix=member")]),t._v(",那么下面注入的"),s("code",[t._v("redis.Cache")]),t._v("接口将使用的前缀为:"),s("code",[t._v("member#points")]),t._v(";执行操作"),s("code",[t._v('r.redis.Set("use-110", "value")')]),t._v("操作,保存到redis中的key为:"),s("code",[t._v("member#points#use-110")]),t._v("。")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n redis redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,points"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseProvidedCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("redis.Locker 和 redis.Key 接口的注入 与 redis.Cache 接口类似。")]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("从配置文件中读取一个值作为前缀来拼接\n如果配置了"),s("code",[t._v("redis.cache.prefix=order")]),t._v(" 和 "),s("code",[t._v("app.order.sell.redis-prefix=sell")]),t._v(",下面注入的接口将使用的前缀为:"),s("code",[t._v("order#sell")]),t._v(";执行操作"),s("code",[t._v('r.redis.Set("use-110", "value")')]),t._v("操作,保存到redis中的key为:"),s("code",[t._v("order#sell#use-110")]),t._v("。")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n redis redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,config=app.order.sell.redis-prefix"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//读取配置中的 app.order.sell.redis-prefix 的值用于创建 redis.Cache")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseProvidedCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("redis.Locker 和 redis.Key 接口的注入 与 redis.Cache 接口类似。")]),t._v(" "),s("ol",{attrs:{start:"3"}},[s("li",[t._v("使用 "),s("code",[t._v("redis.Hash")]),t._v(" 接口\n在注入"),s("code",[t._v("redis.Hash")]),t._v("接口时,key不在为前缀,而就是当前redis要操作的key。")])]),t._v(" "),s("p",[t._v("如果配置了"),s("code",[t._v("redis.cache.prefix=user")]),t._v(",下面代码中的"),s("code",[t._v('r.hash.Set("use-110", "value")')]),t._v("将会被翻译为指令:"),s("code",[t._v("HSET user-set use-110 value")]),t._v("。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n hash redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Hash "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,user-set"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseHash")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("hash"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"直接使用redis连接池"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#直接使用redis连接池"}},[t._v("#")]),t._v(" 直接使用redis连接池")]),t._v(" "),s("h3",{attrs:{id:"接口定义-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-4"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Pool "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取链接")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Conn\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//释放链接")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Close")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conn redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-4"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n pool redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Pool "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-pool"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UsePool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n conn "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pool"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pool"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Close")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//执行redis命令")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"SET"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"key"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[72],{361:function(t,s,a){"use strict";a.r(s);var n=a(10),e=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"redis-参考文档"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#redis-参考文档"}},[t._v("#")]),t._v(" Redis 参考文档")]),t._v(" "),s("h2",{attrs:{id:"配置项"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#配置项"}},[t._v("#")]),t._v(" 配置项")]),t._v(" "),s("ul",[s("li",[t._v("redis.max-idle:最大空闲链接数,不配置默认为2")]),t._v(" "),s("li",[t._v("redis.max-active:最大活跃链接数,不配置默认为10")]),t._v(" "),s("li",[t._v("redis.db:使用的db,不配置默认为0")]),t._v(" "),s("li",[t._v("redis.cache.prefix:key前缀,如果设置了,对redis的增删改查都会拼接该前缀,拼接方式${prefix}#${key};默认为空")])]),t._v(" "),s("h2",{attrs:{id:"import-和-bury"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#import-和-bury"}},[t._v("#")]),t._v(" import 和 bury")]),t._v(" "),s("ul",[s("li",[t._v("import")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("import")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"github.com/gone-io/gone/goner/redis"')]),t._v("\n")])])]),s("ul",[s("li",[t._v("bury")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//使用 goner.RedisPriest 函数,将 redis 相关的Goner 埋葬到 Cemetery 中")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" goner"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("RedisPriest")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("cemetery"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用分布是缓存-redis-cache"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布是缓存-redis-cache"}},[t._v("#")]),t._v(" 使用分布是缓存 redis.Cache")]),t._v(" "),s("h3",{attrs:{id:"接口定义"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Cache "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存,Set的别名")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("...")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" value any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//删除缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Remove")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//根据模式获取缓存的key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Keys")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pattern "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取当前Cache使用的前缀")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prefix")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\n\tcache redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-cache"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tkey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-address"')]),t._v("\n\tvalue "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://github.com/gone-io/gone"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置缓存")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Put")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\tvalue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第二参数为 需要缓存的值,类型为any,可以是任意类型;传入的值会被编码为 `[]byte` 发往redis")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 第三个参数为 过期时间,类型为 `time.Duration`;省略,表示不设置过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取缓存")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("var")]),t._v(" getValue "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("cache"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tkey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 缓存的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("&")]),t._v("getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为指针,接收获取缓存的值,类型为any,可以是任意类型;从redis获取的值会被解码为传入的指针类型")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"getValue:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" getValue"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用分布式锁-redis-locker"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用分布式锁-redis-locker"}},[t._v("#")]),t._v(" 使用分布式锁 redis.Locker")]),t._v(" "),s("h3",{attrs:{id:"接口定义-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-2"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// Locker redis 分布式缓存")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Locker "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//TryLock 尝试将key锁定一段时间,成功返回一个解锁函数,失败返回错误")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("unlock Unlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//LockAndDo 尝试获取锁,并执行fn函数,函数未执行完自动续期,函数执行完后自动释放锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" fn "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" lockTime"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" checkPeriod time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-2"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-2"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tlocker redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Locker "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-locker"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//尝试获取锁 并 锁定一段时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//返回的第一个参数为一个解锁的函数")]),t._v("\n\tunlock"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("TryLock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("10")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二参数为 锁的过期时间,类型为 `time.Duration`")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//操作完后,需要解锁")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("unlock")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockFunc")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tlockKey "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"gone-lock-key"')]),t._v("\n\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("locker"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("LockAndDo")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("\n\t\tlockKey"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第一个参数为 锁的key,类型为 `string`")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第二个参数为 需要执行的函数,类型为 `func()`,代表一个操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取锁成功后,可以进行业务操作")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("println")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"do some options"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("100")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第三个参数为 锁的过期时间,类型为 `time.Duration`;第一次加锁和后续锁续期都将使用该值")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("5")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Second"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//第四个参数为 锁续期的间隔时间,类型为 `time.Duration`;周期性检查所是否将过期,如果在下个周期内会过期则对锁续期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\tfmt"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Printf")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"err:%v"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"操作key-使用-redis-key"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#操作key-使用-redis-key"}},[t._v("#")]),t._v(" 操作Key,使用 redis.Key")]),t._v(" "),s("h3",{attrs:{id:"接口定义-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-3"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Key "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Expire 给key设置一段时间后过期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Expire")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" ttl time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//ExpireAt 设置key在某个时间点过期")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ExpireAt")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" time time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Ttl 获取key的过期时间")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Ttl")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("time"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Duration"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Del 删除一个key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Del")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("key "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Incr 使key自增一个量")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Incr")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" increment "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Keys 根据模式获取缓存的key")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Keys")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("pattern "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//Prefix 获取redis配置的key前缀")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Prefix")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"使用-provider-注入-redis-接口"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用-provider-注入-redis-接口"}},[t._v("#")]),t._v(" 使用 Provider 注入 redis 接口")]),t._v(" "),s("p",[t._v("通过标签 "),s("code",[t._v('gone:"gone-redis-provider,${key}')]),t._v("使用redis Provider 创建一个接口给注入属性。支持注入的接口包括前面的"),s("code",[t._v("redis.Cache")]),t._v("、"),s("code",[t._v("redis.Locker")]),t._v("、"),s("code",[t._v("redis.Key")]),t._v(";另外还支持注入 "),s("code",[t._v("redis.Hash")]),t._v("接口,用于操作redis hash。")]),t._v(" "),s("p",[t._v("Hash接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Hash "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//设置hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//删除hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Del")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//扫描hash字段")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Scan")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" v "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("byte")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//hash字段自增")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Incr")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("field "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" increment "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-3"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-3"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("ol",[s("li",[t._v("使用 Provider 创建一个redis.Cache接口,该缓存接口的前缀将使用配置中的前缀拼接${key}得到新的前缀,可以用于有按模块隔离的场景。\n如果配置了"),s("code",[t._v("redis.cache.prefix=member")]),t._v(",那么下面注入的"),s("code",[t._v("redis.Cache")]),t._v("接口将使用的前缀为:"),s("code",[t._v("member#points")]),t._v(";执行操作"),s("code",[t._v('r.redis.Set("use-110", "value")')]),t._v("操作,保存到redis中的key为:"),s("code",[t._v("member#points#use-110")]),t._v("。")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n redis redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,points"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseProvidedCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("redis.Locker 和 redis.Key 接口的注入 与 redis.Cache 接口类似。")]),t._v(" "),s("ol",{attrs:{start:"2"}},[s("li",[t._v("从配置文件中读取一个值作为前缀来拼接\n如果配置了"),s("code",[t._v("redis.cache.prefix=order")]),t._v(" 和 "),s("code",[t._v("app.order.sell.redis-prefix=sell")]),t._v(",下面注入的接口将使用的前缀为:"),s("code",[t._v("order#sell")]),t._v(";执行操作"),s("code",[t._v('r.redis.Set("use-110", "value")')]),t._v("操作,保存到redis中的key为:"),s("code",[t._v("order#sell#use-110")]),t._v("。")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n redis redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Cache "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,config=app.order.sell.redis-prefix"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//读取配置中的 app.order.sell.redis-prefix 的值用于创建 redis.Cache")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseProvidedCache")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("p",[t._v("redis.Locker 和 redis.Key 接口的注入 与 redis.Cache 接口类似。")]),t._v(" "),s("ol",{attrs:{start:"3"}},[s("li",[t._v("使用 "),s("code",[t._v("redis.Hash")]),t._v(" 接口\n在注入"),s("code",[t._v("redis.Hash")]),t._v("接口时,key不在为前缀,而就是当前redis要操作的key。")])]),t._v(" "),s("p",[t._v("如果配置了"),s("code",[t._v("redis.cache.prefix=user")]),t._v(",下面代码中的"),s("code",[t._v('r.hash.Set("use-110", "value")')]),t._v("将会被翻译为指令:"),s("code",[t._v("HSET user-set use-110 value")]),t._v("。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n hash redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Hash "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-provider,user-set"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UseHash")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("hash"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Set")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"use-110"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"直接使用redis连接池"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#直接使用redis连接池"}},[t._v("#")]),t._v(" 直接使用redis连接池")]),t._v(" "),s("h3",{attrs:{id:"接口定义-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#接口定义-4"}},[t._v("#")]),t._v(" 接口定义")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" Pool "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("interface")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//获取链接")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" Conn\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//释放链接")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Close")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conn redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h3",{attrs:{id:"使用示例-4"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#使用示例-4"}},[t._v("#")]),t._v(" 使用示例")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" redisUser "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n pool redis"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Pool "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-redis-pool"`')]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("r "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("redisUser"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("UsePool")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n conn "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pool"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Get")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("defer")]),t._v(" r"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("pool"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Close")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//执行redis命令")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" conn"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Do")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"SET"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"key"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"value"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])])])}),[],!1,null,null,null);s.default=e.exports}}]); \ No newline at end of file diff --git a/assets/js/73.e80565dd.js b/assets/js/73.f6cf8fed.js similarity index 99% rename from assets/js/73.e80565dd.js rename to assets/js/73.f6cf8fed.js index 010cd8d..6872b9d 100644 --- a/assets/js/73.e80565dd.js +++ b/assets/js/73.f6cf8fed.js @@ -1 +1 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[73],{362:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone对xorm的增强"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone对xorm的增强"}},[t._v("#")]),t._v(" Gone对Xorm的增强")]),t._v(" "),s("h2",{attrs:{id:"_1-自动事务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-自动事务"}},[t._v("#")]),t._v(" 1. 自动事务")]),t._v(" "),s("p",[t._v("使用"),s("code",[t._v("Transaction")]),t._v("函数包裹的函数,执行前会自动开启事务,返回"),s("code",[t._v("error")]),t._v("或者发生"),s("code",[t._v("panic")]),t._v("自动完成事务回滚,不返回"),s("code",[t._v("error")]),t._v("则自动提交事务。")]),t._v(" "),s("blockquote",[s("p",[t._v("注意:在"),s("code",[t._v("Transaction")]),t._v("函数包裹的数据库操作函数需要使用"),s("code",[t._v("session xorm.Interface")]),t._v("执行数据库操作")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" db "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("XormEngine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入数据库引擎")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用Transaction包裹的函数,执行前会自动开启事务")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:使用的session进行数据库操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" session"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ID")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Update")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"_2-事务自动传递"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-事务自动传递"}},[t._v("#")]),t._v(" 2. 事务自动传递")]),t._v(" "),s("p",[t._v("嵌套使用"),s("code",[t._v("Transaction")]),t._v("函数包裹的函数,只会开启一个事务,嵌套事务会自动传递,嵌套事务的"),s("code",[t._v("error")]),t._v("或者发生"),s("code",[t._v("panic")]),t._v("自动完成事务回滚,不返回"),s("code",[t._v("error")]),t._v("则自动提交事务。")]),t._v(" "),s("p",[t._v("这样带来一个好处,让我们编写的函数在组合时能够自动合并到一个事务中。")]),t._v(" "),s("p",[t._v("请看下面代码,如果"),s("code",[t._v("updateUser")]),t._v("、"),s("code",[t._v("updateFriends")]),t._v("函数单独使用,会分别开启事务;将他们嵌套在"),s("code",[t._v("DoUpdate")]),t._v("的"),s("code",[t._v("Transaction")]),t._v("的函数中,则会合并到一个事务中。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" db "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("XormEngine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入数据库引擎")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用Transaction包裹的函数,执行前会自动开启事务")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:使用的session进行数据库操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" session"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ID")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Update")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateFriends")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("userId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Friend"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo: 更新朋友的相关操作")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DoUpdate")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Friend"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" err\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateFriends")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"_3-sql支持名字参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-sql支持名字参数"}},[t._v("#")]),t._v(" 3. SQL支持名字参数")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tsql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MustNamed")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`\n\t\tupdate user\n\t\tset\n\t\t status = :status,\n\t\t\tavatar = :avatar\n\t\twhere\n\t\t id = :id`")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"status"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"avatar"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://wwww...."')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("通过"),s("code",[t._v("xorm.MustNamed")]),t._v("处理后的sql为:")]),t._v(" "),s("div",{staticClass:"language-sql extra-class"},[s("pre",{pre:!0,attrs:{class:"language-sql"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("update")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("user")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("set")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("status")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n avatar "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("where")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?\n")])])]),s("p",[s("code",[t._v("args")]),t._v("为"),s("code",[t._v("[]any")]),t._v("类型,值为:"),s("code",[t._v('1,1,"https://wwww...."')]),t._v("。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file +(window.webpackJsonp=window.webpackJsonp||[]).push([[73],{363:function(t,s,a){"use strict";a.r(s);var n=a(10),r=Object(n.a)({},(function(){var t=this,s=t._self._c;return s("ContentSlotsDistributor",{attrs:{"slot-key":t.$parent.slotKey}},[s("h1",{attrs:{id:"gone对xorm的增强"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#gone对xorm的增强"}},[t._v("#")]),t._v(" Gone对Xorm的增强")]),t._v(" "),s("h2",{attrs:{id:"_1-自动事务"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_1-自动事务"}},[t._v("#")]),t._v(" 1. 自动事务")]),t._v(" "),s("p",[t._v("使用"),s("code",[t._v("Transaction")]),t._v("函数包裹的函数,执行前会自动开启事务,返回"),s("code",[t._v("error")]),t._v("或者发生"),s("code",[t._v("panic")]),t._v("自动完成事务回滚,不返回"),s("code",[t._v("error")]),t._v("则自动提交事务。")]),t._v(" "),s("blockquote",[s("p",[t._v("注意:在"),s("code",[t._v("Transaction")]),t._v("函数包裹的数据库操作函数需要使用"),s("code",[t._v("session xorm.Interface")]),t._v("执行数据库操作")])]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" db "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("XormEngine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入数据库引擎")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用Transaction包裹的函数,执行前会自动开启事务")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:使用的session进行数据库操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" session"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ID")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Update")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"_2-事务自动传递"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_2-事务自动传递"}},[t._v("#")]),t._v(" 2. 事务自动传递")]),t._v(" "),s("p",[t._v("嵌套使用"),s("code",[t._v("Transaction")]),t._v("函数包裹的函数,只会开启一个事务,嵌套事务会自动传递,嵌套事务的"),s("code",[t._v("error")]),t._v("或者发生"),s("code",[t._v("panic")]),t._v("自动完成事务回滚,不返回"),s("code",[t._v("error")]),t._v("则自动提交事务。")]),t._v(" "),s("p",[t._v("这样带来一个好处,让我们编写的函数在组合时能够自动合并到一个事务中。")]),t._v(" "),s("p",[t._v("请看下面代码,如果"),s("code",[t._v("updateUser")]),t._v("、"),s("code",[t._v("updateFriends")]),t._v("函数单独使用,会分别开启事务;将他们嵌套在"),s("code",[t._v("DoUpdate")]),t._v("的"),s("code",[t._v("Transaction")]),t._v("的函数中,则会合并到一个事务中。")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("type")]),t._v(" db "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("struct")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Flag\n\tgone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("XormEngine "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('`gone:"gone-xorm"`')]),t._v(" "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注入数据库引擎")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("// 使用Transaction包裹的函数,执行前会自动开启事务")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\n "),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//注意:使用的session进行数据库操作")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("_")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" session"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ID")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Update")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" gone"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("ToError")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("err"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateFriends")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("userId "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("int64")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Friend"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token comment"}},[t._v("//todo: 更新朋友的相关操作")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("d "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("db"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("DoUpdate")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("User"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("*")]),t._v("entity"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Friend"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("Transaction")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("func")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("session xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Interface"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("error")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\terr "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateUser")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("if")]),t._v(" err "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("!=")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token boolean"}},[t._v("nil")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" err\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("return")]),t._v(" d"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("updateFriends")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),t._v("user"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),t._v("Id"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" friedns"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),t._v("\n")])])]),s("h2",{attrs:{id:"_3-sql支持名字参数"}},[s("a",{staticClass:"header-anchor",attrs:{href:"#_3-sql支持名字参数"}},[t._v("#")]),t._v(" 3. SQL支持名字参数")]),t._v(" "),s("div",{staticClass:"language-go extra-class"},[s("pre",{pre:!0,attrs:{class:"language-go"}},[s("code",[t._v("\tsql"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v(" args "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v(":=")]),t._v(" xorm"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(".")]),s("span",{pre:!0,attrs:{class:"token function"}},[t._v("MustNamed")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("(")]),s("span",{pre:!0,attrs:{class:"token string"}},[t._v("`\n\t\tupdate user\n\t\tset\n\t\t status = :status,\n\t\t\tavatar = :avatar\n\t\twhere\n\t\t id = :id`")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("map")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("[")]),s("span",{pre:!0,attrs:{class:"token builtin"}},[t._v("string")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("]")]),t._v("any"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("{")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"id"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"status"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token number"}},[t._v("1")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t\t"),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"avatar"')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(":")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token string"}},[t._v('"https://wwww...."')]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v("}")]),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n\t"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(")")]),t._v("\n")])])]),s("p",[t._v("通过"),s("code",[t._v("xorm.MustNamed")]),t._v("处理后的sql为:")]),t._v(" "),s("div",{staticClass:"language-sql extra-class"},[s("pre",{pre:!0,attrs:{class:"language-sql"}},[s("code",[s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("update")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("user")]),t._v("\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("set")]),t._v("\n "),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("status")]),t._v(" "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?"),s("span",{pre:!0,attrs:{class:"token punctuation"}},[t._v(",")]),t._v("\n avatar "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?\n"),s("span",{pre:!0,attrs:{class:"token keyword"}},[t._v("where")]),t._v("\n id "),s("span",{pre:!0,attrs:{class:"token operator"}},[t._v("=")]),t._v(" ?\n")])])]),s("p",[s("code",[t._v("args")]),t._v("为"),s("code",[t._v("[]any")]),t._v("类型,值为:"),s("code",[t._v('1,1,"https://wwww...."')]),t._v("。")])])}),[],!1,null,null,null);s.default=r.exports}}]); \ No newline at end of file diff --git a/assets/js/app.0fd43cce.js b/assets/js/app.ab6c9e5c.js similarity index 73% rename from assets/js/app.0fd43cce.js rename to assets/js/app.ab6c9e5c.js index 2d1b9b6..863fbc5 100644 --- a/assets/js/app.0fd43cce.js +++ b/assets/js/app.ab6c9e5c.js @@ -1,4 +1,4 @@ -(window.webpackJsonp=window.webpackJsonp||[]).push([[0],[]]);!function(e){function t(t){for(var r,a,s=t[0],l=t[1],u=t[2],f=0,p=[];f
'};function o(e,t,n){return en?n:e}function i(e){return 100*(-1+e)}n.configure=function(e){var t,n;for(t in e)void 0!==(n=e[t])&&e.hasOwnProperty(t)&&(r[t]=n);return this},n.status=null,n.set=function(e){var t=n.isStarted();e=o(e,r.minimum,1),n.status=1===e?null:e;var l=n.render(!t),u=l.querySelector(r.barSelector),c=r.speed,f=r.easing;return l.offsetWidth,a((function(t){""===r.positionUsing&&(r.positionUsing=n.getPositioningCSS()),s(u,function(e,t,n){var o;return(o="translate3d"===r.positionUsing?{transform:"translate3d("+i(e)+"%,0,0)"}:"translate"===r.positionUsing?{transform:"translate("+i(e)+"%,0)"}:{"margin-left":i(e)+"%"}).transition="all "+t+"ms "+n,o}(e,c,f)),1===e?(s(l,{transition:"none",opacity:1}),l.offsetWidth,setTimeout((function(){s(l,{transition:"all "+c+"ms linear",opacity:0}),setTimeout((function(){n.remove(),t()}),c)}),c)):setTimeout(t,c)})),this},n.isStarted=function(){return"number"==typeof n.status},n.start=function(){n.status||n.set(0);var e=function(){setTimeout((function(){n.status&&(n.trickle(),e())}),r.trickleSpeed)};return r.trickle&&e(),this},n.done=function(e){return e||n.status?n.inc(.3+.5*Math.random()).set(1):this},n.inc=function(e){var t=n.status;return t?("number"!=typeof e&&(e=(1-t)*o(Math.random()*t,.1,.95)),t=o(t+e,0,.994),n.set(t)):n.start()},n.trickle=function(){return n.inc(Math.random()*r.trickleRate)},e=0,t=0,n.promise=function(r){return r&&"resolved"!==r.state()?(0===t&&n.start(),e++,t++,r.always((function(){0==--t?(e=0,n.done()):n.set((e-t)/e)})),this):this},n.render=function(e){if(n.isRendered())return document.getElementById("nprogress");u(document.documentElement,"nprogress-busy");var t=document.createElement("div");t.id="nprogress",t.innerHTML=r.template;var o,a=t.querySelector(r.barSelector),l=e?"-100":i(n.status||0),c=document.querySelector(r.parent);return s(a,{transition:"all 0 linear",transform:"translate3d("+l+"%,0,0)"}),r.showSpinner||(o=t.querySelector(r.spinnerSelector))&&p(o),c!=document.body&&u(c,"nprogress-custom-parent"),c.appendChild(t),t},n.remove=function(){c(document.documentElement,"nprogress-busy"),c(document.querySelector(r.parent),"nprogress-custom-parent");var e=document.getElementById("nprogress");e&&p(e)},n.isRendered=function(){return!!document.getElementById("nprogress")},n.getPositioningCSS=function(){var e=document.body.style,t="WebkitTransform"in e?"Webkit":"MozTransform"in e?"Moz":"msTransform"in e?"ms":"OTransform"in e?"O":"";return t+"Perspective"in e?"translate3d":t+"Transform"in e?"translate":"margin"};var a=function(){var e=[];function t(){var n=e.shift();n&&n(t)}return function(n){e.push(n),1==e.length&&t()}}(),s=function(){var e=["Webkit","O","Moz","ms"],t={};function n(n){return n=n.replace(/^-ms-/,"ms-").replace(/-([\da-z])/gi,(function(e,t){return t.toUpperCase()})),t[n]||(t[n]=function(t){var n=document.body.style;if(t in n)return t;for(var r,o=e.length,i=t.charAt(0).toUpperCase()+t.slice(1);o--;)if((r=e[o]+i)in n)return r;return t}(n))}function r(e,t,r){t=n(t),e.style[t]=r}return function(e,t){var n,o,i=arguments;if(2==i.length)for(n in t)void 0!==(o=t[n])&&t.hasOwnProperty(n)&&r(e,n,o);else r(e,i[1],i[2])}}();function l(e,t){return("string"==typeof e?e:f(e)).indexOf(" "+t+" ")>=0}function u(e,t){var n=f(e),r=n+t;l(n,t)||(e.className=r.substring(1))}function c(e,t){var n,r=f(e);l(e,t)&&(n=r.replace(" "+t+" "," "),e.className=n.substring(1,n.length-1))}function f(e){return(" "+(e.className||"")+" ").replace(/\s+/gi," ")}function p(e){e&&e.parentNode&&e.parentNode.removeChild(e)}return n})?r.call(t,n,t,e):r)||(e.exports=o)},function(e,t,n){"use strict";var r=n(0),o=n(46).f,i=n(13),a=n(93),s=n(33),l=n(61),u=n(121);e.exports=function(e,t){var n,c,f,p,h,d=e.target,v=e.global,g=e.stat;if(n=v?r:g?r[d]||s(d,{}):r[d]&&r[d].prototype)for(c in t){if(p=t[c],f=e.dontCallGetSet?(h=o(n,c))&&h.value:n[c],!u(v?c:d+(g?".":"#")+c,e.forced)&&void 0!==f){if(typeof p==typeof f)continue;l(p,f)}(e.sham||f&&f.sham)&&i(p,"sham",!0),a(n,c,p,e)}}},function(e,t,n){"use strict";var r=n(25),o=Function.prototype.call;e.exports=r?o.bind(o):function(){return o.apply(o,arguments)}},function(e,t,n){"use strict";var r=n(3);e.exports=!r((function(){var e=function(){}.bind();return"function"!=typeof e||e.hasOwnProperty("prototype")}))},function(e,t,n){"use strict";e.exports=function(e,t){return{enumerable:!(1&e),configurable:!(2&e),writable:!(4&e),value:t}}},function(e,t,n){"use strict";var r=n(47),o=n(28);e.exports=function(e){return r(o(e))}},function(e,t,n){"use strict";var r=n(48),o=TypeError;e.exports=function(e){if(r(e))throw new o("Can't call method on "+e);return e}},function(e,t,n){"use strict";var r=n(0),o=n(1),i=function(e){return o(e)?e:void 0};e.exports=function(e,t){return arguments.length<2?i(r[e]):r[e]&&r[e][t]}},function(e,t,n){"use strict";var r=n(1),o=n(103),i=TypeError;e.exports=function(e){if(r(e))return e;throw new i(o(e)+" is not a function")}},function(e,t,n){"use strict";var r=n(0),o=n(55),i=n(8),a=n(57),s=n(53),l=n(52),u=r.Symbol,c=o("wks"),f=l?u.for||u:u&&u.withoutSetter||a;e.exports=function(e){return i(c,e)||(c[e]=s&&i(u,e)?u[e]:f("Symbol."+e)),c[e]}},function(e,t,n){"use strict";var r=n(56),o=n(0),i=n(33),a=e.exports=o["__core-js_shared__"]||i("__core-js_shared__",{});(a.versions||(a.versions=[])).push({version:"3.37.1",mode:r?"pure":"global",copyright:"© 2014-2024 Denis Pushkarev (zloirock.ru)",license:"https://github.com/zloirock/core-js/blob/v3.37.1/LICENSE",source:"https://github.com/zloirock/core-js"})},function(e,t,n){"use strict";var r=n(0),o=Object.defineProperty;e.exports=function(e,t){try{o(r,e,{value:t,configurable:!0,writable:!0})}catch(n){r[e]=t}return t}},function(e,t,n){"use strict";var r=n(28),o=Object;e.exports=function(e){return o(r(e))}},function(e,t,n){"use strict";var r=n(118);e.exports=function(e){return r(e.length)}},function(e,t,n){var r=n(145),o=n(11),i=Object.prototype,a=i.hasOwnProperty,s=i.propertyIsEnumerable,l=r(function(){return arguments}())?r:function(e){return o(e)&&a.call(e,"callee")&&!s.call(e,"callee")};e.exports=l},function(e,t,n){var r=n(9)(n(7),"Map");e.exports=r},function(e,t){e.exports=function(e){var t=typeof e;return null!=e&&("object"==t||"function"==t)}},function(e,t,n){var r=n(165),o=n(172),i=n(174),a=n(175),s=n(176);function l(e){var t=-1,n=null==e?0:e.length;for(this.clear();++t-1&&e%1==0&&e<=9007199254740991}},function(e,t,n){var r=n(4),o=n(43),i=/\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,a=/^\w*$/;e.exports=function(e,t){if(r(e))return!1;var n=typeof e;return!("number"!=n&&"symbol"!=n&&"boolean"!=n&&null!=e&&!o(e))||(a.test(e)||!i.test(e)||null!=t&&e in Object(t))}},function(e,t,n){var r=n(12),o=n(11);e.exports=function(e){return"symbol"==typeof e||o(e)&&"[object Symbol]"==r(e)}},function(e,t){e.exports=function(e){return e}},function(e,t){function n(e,t){for(var n=0,r=e.length-1;r>=0;r--){var o=e[r];"."===o?e.splice(r,1):".."===o?(e.splice(r,1),n++):n&&(e.splice(r,1),n--)}if(t)for(;n--;n)e.unshift("..");return e}function r(e,t){if(e.filter)return e.filter(t);for(var n=[],r=0;r=-1&&!t;o--){var i=o>=0?arguments[o]:process.cwd();if("string"!=typeof i)throw new TypeError("Arguments to path.resolve must be strings");i&&(e=i+"/"+e,t="/"===i.charAt(0))}return(t?"/":"")+(e=n(r(e.split("/"),(function(e){return!!e})),!t).join("/"))||"."},t.normalize=function(e){var i=t.isAbsolute(e),a="/"===o(e,-1);return(e=n(r(e.split("/"),(function(e){return!!e})),!i).join("/"))||i||(e="."),e&&a&&(e+="/"),(i?"/":"")+e},t.isAbsolute=function(e){return"/"===e.charAt(0)},t.join=function(){var e=Array.prototype.slice.call(arguments,0);return t.normalize(r(e,(function(e,t){if("string"!=typeof e)throw new TypeError("Arguments to path.join must be strings");return e})).join("/"))},t.relative=function(e,n){function r(e){for(var t=0;t=0&&""===e[n];n--);return t>n?[]:e.slice(t,n-t+1)}e=t.resolve(e).substr(1),n=t.resolve(n).substr(1);for(var o=r(e.split("/")),i=r(n.split("/")),a=Math.min(o.length,i.length),s=a,l=0;l=1;--i)if(47===(t=e.charCodeAt(i))){if(!o){r=i;break}}else o=!1;return-1===r?n?"/":".":n&&1===r?"/":e.slice(0,r)},t.basename=function(e,t){var n=function(e){"string"!=typeof e&&(e+="");var t,n=0,r=-1,o=!0;for(t=e.length-1;t>=0;--t)if(47===e.charCodeAt(t)){if(!o){n=t+1;break}}else-1===r&&(o=!1,r=t+1);return-1===r?"":e.slice(n,r)}(e);return t&&n.substr(-1*t.length)===t&&(n=n.substr(0,n.length-t.length)),n},t.extname=function(e){"string"!=typeof e&&(e+="");for(var t=-1,n=0,r=-1,o=!0,i=0,a=e.length-1;a>=0;--a){var s=e.charCodeAt(a);if(47!==s)-1===r&&(o=!1,r=a+1),46===s?-1===t?t=a:1!==i&&(i=1):-1!==t&&(i=-1);else if(!o){n=a+1;break}}return-1===t||-1===r||0===i||1===i&&t===r-1&&t===n+1?"":e.slice(t,r)};var o="b"==="ab".substr(-1)?function(e,t,n){return e.substr(t,n)}:function(e,t,n){return t<0&&(t=e.length+t),e.substr(t,n)}},function(e,t,n){"use strict";var r=n(5),o=n(24),i=n(99),a=n(26),s=n(27),l=n(49),u=n(8),c=n(58),f=Object.getOwnPropertyDescriptor;t.f=r?f:function(e,t){if(e=s(e),t=l(t),c)try{return f(e,t)}catch(e){}if(u(e,t))return a(!o(i.f,e,t),e[t])}},function(e,t,n){"use strict";var r=n(2),o=n(3),i=n(16),a=Object,s=r("".split);e.exports=o((function(){return!a("z").propertyIsEnumerable(0)}))?function(e){return"String"===i(e)?s(e,""):a(e)}:a},function(e,t,n){"use strict";e.exports=function(e){return null==e}},function(e,t,n){"use strict";var r=n(100),o=n(50);e.exports=function(e){var t=r(e,"string");return o(t)?t:t+""}},function(e,t,n){"use strict";var r=n(29),o=n(1),i=n(51),a=n(52),s=Object;e.exports=a?function(e){return"symbol"==typeof e}:function(e){var t=r("Symbol");return o(t)&&i(t.prototype,s(e))}},function(e,t,n){"use strict";var r=n(2);e.exports=r({}.isPrototypeOf)},function(e,t,n){"use strict";var r=n(53);e.exports=r&&!Symbol.sham&&"symbol"==typeof Symbol.iterator},function(e,t,n){"use strict";var r=n(54),o=n(3),i=n(0).String;e.exports=!!Object.getOwnPropertySymbols&&!o((function(){var e=Symbol("symbol detection");return!i(e)||!(Object(e)instanceof Symbol)||!Symbol.sham&&r&&r<41}))},function(e,t,n){"use strict";var r,o,i=n(0),a=n(101),s=i.process,l=i.Deno,u=s&&s.versions||l&&l.version,c=u&&u.v8;c&&(o=(r=c.split("."))[0]>0&&r[0]<4?1:+(r[0]+r[1])),!o&&a&&(!(r=a.match(/Edge\/(\d+)/))||r[1]>=74)&&(r=a.match(/Chrome\/(\d+)/))&&(o=+r[1]),e.exports=o},function(e,t,n){"use strict";var r=n(32);e.exports=function(e,t){return r[e]||(r[e]=t||{})}},function(e,t,n){"use strict";e.exports=!1},function(e,t,n){"use strict";var r=n(2),o=0,i=Math.random(),a=r(1..toString);e.exports=function(e){return"Symbol("+(void 0===e?"":e)+")_"+a(++o+i,36)}},function(e,t,n){"use strict";var r=n(5),o=n(3),i=n(105);e.exports=!r&&!o((function(){return 7!==Object.defineProperty(i("div"),"a",{get:function(){return 7}}).a}))},function(e,t,n){"use strict";var r=n(6),o=String,i=TypeError;e.exports=function(e){if(r(e))return e;throw new i(o(e)+" is not an object")}},function(e,t,n){"use strict";e.exports={}},function(e,t,n){"use strict";var r=n(8),o=n(112),i=n(46),a=n(15);e.exports=function(e,t,n){for(var s=o(t),l=a.f,u=i.f,c=0;cc))return!1;var p=l.get(e),h=l.get(t);if(p&&h)return p==t&&h==e;var d=-1,v=!0,g=2&n?new r:void 0;for(l.set(e,t),l.set(t,e);++d-1&&e%1==0&&e=0&&Math.floor(t)===t&&isFinite(e)}function v(e){return a(e)&&"function"==typeof e.then&&"function"==typeof e.catch}function g(e){return null==e?"":Array.isArray(e)||p(e)&&e.toString===f?JSON.stringify(e,m,2):String(e)}function m(e,t){return t&&t.__v_isRef?t.value:t}function y(e){var t=parseFloat(e);return isNaN(t)?e:t}function b(e,t){for(var n=Object.create(null),r=e.split(","),o=0;o-1)return e.splice(r,1)}}var w=Object.prototype.hasOwnProperty;function k(e,t){return w.call(e,t)}function j(e){var t=Object.create(null);return function(n){return t[n]||(t[n]=e(n))}}var C=/-(\w)/g,P=j((function(e){return e.replace(C,(function(e,t){return t?t.toUpperCase():""}))})),S=j((function(e){return e.charAt(0).toUpperCase()+e.slice(1)})),O=/\B([A-Z])/g,$=j((function(e){return e.replace(O,"-$1").toLowerCase()}));var E=Function.prototype.bind?function(e,t){return e.bind(t)}:function(e,t){function n(n){var r=arguments.length;return r?r>1?e.apply(t,arguments):e.call(t,n):e.call(t)}return n._length=e.length,n};function T(e,t){t=t||0;for(var n=e.length-t,r=new Array(n);n--;)r[n]=e[n+t];return r}function A(e,t){for(var n in t)e[n]=t[n];return e}function z(e){for(var t={},n=0;n0,Z=X&&X.indexOf("edge/")>0;X&&X.indexOf("android");var ee=X&&/iphone|ipad|ipod|ios/.test(X);X&&/chrome\/\d+/.test(X),X&&/phantomjs/.test(X);var te,ne=X&&X.match(/firefox\/(\d+)/),re={}.watch,oe=!1;if(Q)try{var ie={};Object.defineProperty(ie,"passive",{get:function(){oe=!0}}),window.addEventListener("test-passive",null,ie)}catch(e){}var ae=function(){return void 0===te&&(te=!Q&&"undefined"!=typeof global&&(global.process&&"server"===global.process.env.VUE_ENV)),te},se=Q&&window.__VUE_DEVTOOLS_GLOBAL_HOOK__;function le(e){return"function"==typeof e&&/native code/.test(e.toString())}var ue,ce="undefined"!=typeof Symbol&&le(Symbol)&&"undefined"!=typeof Reflect&&le(Reflect.ownKeys);ue="undefined"!=typeof Set&&le(Set)?Set:function(){function e(){this.set=Object.create(null)}return e.prototype.has=function(e){return!0===this.set[e]},e.prototype.add=function(e){this.set[e]=!0},e.prototype.clear=function(){this.set=Object.create(null)},e}();var fe=null;function pe(e){void 0===e&&(e=null),e||fe&&fe._scope.off(),fe=e,e&&e._scope.on()}var he=function(){function e(e,t,n,r,o,i,a,s){this.tag=e,this.data=t,this.children=n,this.text=r,this.elm=o,this.ns=void 0,this.context=i,this.fnContext=void 0,this.fnOptions=void 0,this.fnScopeId=void 0,this.key=t&&t.key,this.componentOptions=a,this.componentInstance=void 0,this.parent=void 0,this.raw=!1,this.isStatic=!1,this.isRootInsert=!0,this.isComment=!1,this.isCloned=!1,this.isOnce=!1,this.asyncFactory=s,this.asyncMeta=void 0,this.isAsyncPlaceholder=!1}return Object.defineProperty(e.prototype,"child",{get:function(){return this.componentInstance},enumerable:!1,configurable:!0}),e}(),de=function(e){void 0===e&&(e="");var t=new he;return t.text=e,t.isComment=!0,t};function ve(e){return new he(void 0,void 0,void 0,String(e))}function ge(e){var t=new he(e.tag,e.data,e.children&&e.children.slice(),e.text,e.elm,e.context,e.componentOptions,e.asyncFactory);return t.ns=e.ns,t.isStatic=e.isStatic,t.key=e.key,t.isComment=e.isComment,t.fnContext=e.fnContext,t.fnOptions=e.fnOptions,t.fnScopeId=e.fnScopeId,t.asyncMeta=e.asyncMeta,t.isCloned=!0,t}"function"==typeof SuppressedError&&SuppressedError;var me=0,ye=[],be=function(){function e(){this._pending=!1,this.id=me++,this.subs=[]}return e.prototype.addSub=function(e){this.subs.push(e)},e.prototype.removeSub=function(e){this.subs[this.subs.indexOf(e)]=null,this._pending||(this._pending=!0,ye.push(this))},e.prototype.depend=function(t){e.target&&e.target.addDep(this)},e.prototype.notify=function(e){var t=this.subs.filter((function(e){return e}));for(var n=0,r=t.length;n0&&(Xe((u=e(u,"".concat(n||"","_").concat(r)))[0])&&Xe(f)&&(p[c]=ve(f.text+u[0].text),u.shift()),p.push.apply(p,u)):l(u)?Xe(f)?p[c]=ve(f.text+u):""!==u&&p.push(ve(u)):Xe(u)&&Xe(f)?p[c]=ve(f.text+u.text):(s(t._isVList)&&a(u.tag)&&i(u.key)&&a(n)&&(u.key="__vlist".concat(n,"_").concat(r,"__")),p.push(u)));return p}(e):void 0}function Xe(e){return a(e)&&a(e.text)&&!1===e.isComment}function Je(e,t){var n,r,i,s,l=null;if(o(e)||"string"==typeof e)for(l=new Array(e.length),n=0,r=e.length;n0,s=t?!!t.$stable:!a,l=t&&t.$key;if(t){if(t._normalized)return t._normalized;if(s&&o&&o!==r&&l===o.$key&&!a&&!o.$hasNormal)return o;for(var u in i={},t)t[u]&&"$"!==u[0]&&(i[u]=gt(e,n,u,t[u]))}else i={};for(var c in n)c in i||(i[c]=mt(n,c));return t&&Object.isExtensible(t)&&(t._normalized=i),V(i,"$stable",s),V(i,"$key",l),V(i,"$hasNormal",a),i}function gt(e,t,n,r){var i=function(){var t=fe;pe(e);var n=arguments.length?r.apply(null,arguments):r({}),i=(n=n&&"object"==typeof n&&!o(n)?[n]:Qe(n))&&n[0];return pe(t),n&&(!i||1===n.length&&i.isComment&&!dt(i))?void 0:n};return r.proxy&&Object.defineProperty(t,n,{get:i,enumerable:!0,configurable:!0}),i}function mt(e,t){return function(){return e[t]}}function yt(e){return{get attrs(){if(!e._attrsProxy){var t=e._attrsProxy={};V(t,"_v_attr_proxy",!0),bt(t,e.$attrs,r,e,"$attrs")}return e._attrsProxy},get listeners(){e._listenersProxy||bt(e._listenersProxy={},e.$listeners,r,e,"$listeners");return e._listenersProxy},get slots(){return function(e){e._slotsProxy||xt(e._slotsProxy={},e.$scopedSlots);return e._slotsProxy}(e)},emit:E(e.$emit,e),expose:function(t){t&&Object.keys(t).forEach((function(n){return Ge(e,t,n)}))}}}function bt(e,t,n,r,o){var i=!1;for(var a in t)a in e?t[a]!==n[a]&&(i=!0):(i=!0,_t(e,a,r,o));for(var a in e)a in t||(i=!0,delete e[a]);return i}function _t(e,t,n,r){Object.defineProperty(e,t,{enumerable:!0,configurable:!0,get:function(){return n[r][t]}})}function xt(e,t){for(var n in t)e[n]=t[n];for(var n in e)n in t||delete e[n]}var wt=null;function kt(e,t){return(e.__esModule||ce&&"Module"===e[Symbol.toStringTag])&&(e=e.default),c(e)?t.extend(e):e}function jt(e){if(o(e))for(var t=0;tdocument.createEvent("Event").timeStamp&&(un=function(){return cn.now()})}var fn=function(e,t){if(e.post){if(!t.post)return 1}else if(t.post)return-1;return e.id-t.id};function pn(){var e,t;for(ln=un(),an=!0,tn.sort(fn),sn=0;snsn&&tn[n].id>e.id;)n--;tn.splice(n+1,0,e)}else tn.push(e);on||(on=!0,qt(pn))}}function dn(e,t){if(e){for(var n=Object.create(null),r=ce?Reflect.ownKeys(e):Object.keys(e),o=0;o-1)if(i&&!k(o,"default"))a=!1;else if(""===a||a===$(e)){var l=Rn(String,o.type);(l<0||s-1:"string"==typeof e?e.split(",").indexOf(t)>-1:!!h(e)&&e.test(t)}function Jn(e,t){var n=e.cache,r=e.keys,o=e._vnode,i=e.$vnode;for(var a in n){var s=n[a];if(s){var l=s.name;l&&!t(l)&&Yn(n,a,r,o)}}i.componentOptions.children=void 0}function Yn(e,t,n,r){var o=e[t];!o||r&&o.tag===r.tag||o.componentInstance.$destroy(),e[t]=null,x(n,t)}Wn.prototype._init=function(e){var t=this;t._uid=Hn++,t._isVue=!0,t.__v_skip=!0,t._scope=new Fe(!0),t._scope.parent=void 0,t._scope._vm=!0,e&&e._isComponent?function(e,t){var n=e.$options=Object.create(e.constructor.options),r=t._parentVnode;n.parent=t.parent,n._parentVnode=r;var o=r.componentOptions;n.propsData=o.propsData,n._parentListeners=o.listeners,n._renderChildren=o.children,n._componentTag=o.tag,t.render&&(n.render=t.render,n.staticRenderFns=t.staticRenderFns)}(t,e):t.$options=En(Vn(t.constructor),e||{},t),t._renderProxy=t,t._self=t,function(e){var t=e.$options,n=t.parent;if(n&&!t.abstract){for(;n.$options.abstract&&n.$parent;)n=n.$parent;n.$children.push(e)}e.$parent=n,e.$root=n?n.$root:e,e.$children=[],e.$refs={},e._provided=n?n._provided:Object.create(null),e._watcher=null,e._inactive=null,e._directInactive=!1,e._isMounted=!1,e._isDestroyed=!1,e._isBeingDestroyed=!1}(t),function(e){e._events=Object.create(null),e._hasHookEvent=!1;var t=e.$options._parentListeners;t&&Qt(e,t)}(t),function(e){e._vnode=null,e._staticTrees=null;var t=e.$options,n=e.$vnode=t._parentVnode,o=n&&n.context;e.$slots=pt(t._renderChildren,o),e.$scopedSlots=n?vt(e.$parent,n.data.scopedSlots,e.$slots):r,e._c=function(t,n,r,o){return Ct(e,t,n,r,o,!1)},e.$createElement=function(t,n,r,o){return Ct(e,t,n,r,o,!0)};var i=n&&n.data;Ae(e,"$attrs",i&&i.attrs||r,null,!0),Ae(e,"$listeners",t._parentListeners||r,null,!0)}(t),en(t,"beforeCreate",void 0,!1),function(e){var t=dn(e.$options.inject,e);t&&(Oe(!1),Object.keys(t).forEach((function(n){Ae(e,n,t[n])})),Oe(!0))}(t),qn(t),function(e){var t=e.$options.provide;if(t){var n=u(t)?t.call(e):t;if(!c(n))return;for(var r=Ue(e),o=ce?Reflect.ownKeys(n):Object.keys(n),i=0;i1?T(n):n;for(var r=T(arguments,1),o='event handler for "'.concat(e,'"'),i=0,a=n.length;iparseInt(this.max)&&Yn(e,t[0],t,this._vnode),this.vnodeToCache=null}}},created:function(){this.cache=Object.create(null),this.keys=[]},destroyed:function(){for(var e in this.cache)Yn(this.cache,e,this.keys)},mounted:function(){var e=this;this.cacheVNode(),this.$watch("include",(function(t){Jn(e,(function(e){return Xn(t,e)}))})),this.$watch("exclude",(function(t){Jn(e,(function(e){return!Xn(t,e)}))}))},updated:function(){this.cacheVNode()},render:function(){var e=this.$slots.default,t=jt(e),n=t&&t.componentOptions;if(n){var r=Qn(n),o=this.include,i=this.exclude;if(o&&(!r||!Xn(o,r))||i&&r&&Xn(i,r))return t;var a=this.cache,s=this.keys,l=null==t.key?n.Ctor.cid+(n.tag?"::".concat(n.tag):""):t.key;a[l]?(t.componentInstance=a[l].componentInstance,x(s,l),s.push(l)):(this.vnodeToCache=t,this.keyToCache=l),t.data.keepAlive=!0}return t||e&&e[0]}}};!function(e){var t={get:function(){return U}};Object.defineProperty(e,"config",t),e.util={warn:kn,extend:A,mergeOptions:En,defineReactive:Ae},e.set=ze,e.delete=Le,e.nextTick=qt,e.observable=function(e){return Te(e),e},e.options=Object.create(null),N.forEach((function(t){e.options[t+"s"]=Object.create(null)})),e.options._base=e,A(e.options.components,er),function(e){e.use=function(e){var t=this._installedPlugins||(this._installedPlugins=[]);if(t.indexOf(e)>-1)return this;var n=T(arguments,1);return n.unshift(this),u(e.install)?e.install.apply(e,n):u(e)&&e.apply(null,n),t.push(e),this}}(e),function(e){e.mixin=function(e){return this.options=En(this.options,e),this}}(e),Kn(e),function(e){N.forEach((function(t){e[t]=function(e,n){return n?("component"===t&&p(n)&&(n.name=n.name||e,n=this.options._base.extend(n)),"directive"===t&&u(n)&&(n={bind:n,update:n}),this.options[t+"s"][e]=n,n):this.options[t+"s"][e]}}))}(e)}(Wn),Object.defineProperty(Wn.prototype,"$isServer",{get:ae}),Object.defineProperty(Wn.prototype,"$ssrContext",{get:function(){return this.$vnode&&this.$vnode.ssrContext}}),Object.defineProperty(Wn,"FunctionalRenderContext",{value:vn}),Wn.version="2.7.16";var tr=b("style,class"),nr=b("input,textarea,option,select,progress"),rr=b("contenteditable,draggable,spellcheck"),or=b("events,caret,typing,plaintext-only"),ir=b("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,defaultchecked,defaultmuted,defaultselected,defer,disabled,enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,truespeed,typemustmatch,visible"),ar="http://www.w3.org/1999/xlink",sr=function(e){return":"===e.charAt(5)&&"xlink"===e.slice(0,5)},lr=function(e){return sr(e)?e.slice(6,e.length):""},ur=function(e){return null==e||!1===e};function cr(e){for(var t=e.data,n=e,r=e;a(r.componentInstance);)(r=r.componentInstance._vnode)&&r.data&&(t=fr(r.data,t));for(;a(n=n.parent);)n&&n.data&&(t=fr(t,n.data));return function(e,t){if(a(e)||a(t))return pr(e,hr(t));return""}(t.staticClass,t.class)}function fr(e,t){return{staticClass:pr(e.staticClass,t.staticClass),class:a(e.class)?[e.class,t.class]:t.class}}function pr(e,t){return e?t?e+" "+t:e:t||""}function hr(e){return Array.isArray(e)?function(e){for(var t,n="",r=0,o=e.length;r-1?Mr(e,t,n):ir(t)?ur(n)?e.removeAttribute(t):(n="allowfullscreen"===t&&"EMBED"===e.tagName?"true":t,e.setAttribute(t,n)):rr(t)?e.setAttribute(t,function(e,t){return ur(t)||"false"===t?"false":"contenteditable"===e&&or(t)?t:"true"}(t,n)):sr(t)?ur(n)?e.removeAttributeNS(ar,lr(t)):e.setAttributeNS(ar,t,n):Mr(e,t,n)}function Mr(e,t,n){if(ur(n))e.removeAttribute(t);else{if(J&&!Y&&"TEXTAREA"===e.tagName&&"placeholder"===t&&""!==n&&!e.__ieph){var r=function(t){t.stopImmediatePropagation(),e.removeEventListener("input",r)};e.addEventListener("input",r),e.__ieph=!0}e.setAttribute(t,n)}}var Dr={create:Ir,update:Ir};function qr(e,t){var n=t.elm,r=t.data,o=e.data;if(!(i(r.staticClass)&&i(r.class)&&(i(o)||i(o.staticClass)&&i(o.class)))){var s=cr(t),l=n._transitionClasses;a(l)&&(s=pr(s,hr(l))),s!==n._prevClass&&(n.setAttribute("class",s),n._prevClass=s)}}var Gr,Nr={create:qr,update:qr};function Fr(e,t,n){var r=Gr;return function o(){var i=t.apply(null,arguments);null!==i&&Hr(e,o,n,r)}}var Ur=Tt&&!(ne&&Number(ne[1])<=53);function Br(e,t,n,r){if(Ur){var o=ln,i=t;t=i._wrapper=function(e){if(e.target===e.currentTarget||e.timeStamp>=o||e.timeStamp<=0||e.target.ownerDocument!==document)return i.apply(this,arguments)}}Gr.addEventListener(e,t,oe?{capture:n,passive:r}:n)}function Hr(e,t,n,r){(r||Gr).removeEventListener(e,t._wrapper||t,n)}function Vr(e,t){if(!i(e.data.on)||!i(t.data.on)){var n=t.data.on||{},r=e.data.on||{};Gr=t.elm||e.elm,function(e){if(a(e.__r)){var t=J?"change":"input";e[t]=[].concat(e.__r,e[t]||[]),delete e.__r}a(e.__c)&&(e.change=[].concat(e.__c,e.change||[]),delete e.__c)}(n),Ve(n,r,Br,Hr,Fr,t.context),Gr=void 0}}var Wr,Kr={create:Vr,update:Vr,destroy:function(e){return Vr(e,jr)}};function Qr(e,t){if(!i(e.data.domProps)||!i(t.data.domProps)){var n,r,o=t.elm,l=e.data.domProps||{},u=t.data.domProps||{};for(n in(a(u.__ob__)||s(u._v_attr_proxy))&&(u=t.data.domProps=A({},u)),l)n in u||(o[n]="");for(n in u){if(r=u[n],"textContent"===n||"innerHTML"===n){if(t.children&&(t.children.length=0),r===l[n])continue;1===o.childNodes.length&&o.removeChild(o.childNodes[0])}if("value"===n&&"PROGRESS"!==o.tagName){o._value=r;var c=i(r)?"":String(r);Xr(o,c)&&(o.value=c)}else if("innerHTML"===n&&gr(o.tagName)&&i(o.innerHTML)){(Wr=Wr||document.createElement("div")).innerHTML="".concat(r,"");for(var f=Wr.firstChild;o.firstChild;)o.removeChild(o.firstChild);for(;f.firstChild;)o.appendChild(f.firstChild)}else if(r!==l[n])try{o[n]=r}catch(e){}}}}function Xr(e,t){return!e.composing&&("OPTION"===e.tagName||function(e,t){var n=!0;try{n=document.activeElement!==e}catch(e){}return n&&e.value!==t}(e,t)||function(e,t){var n=e.value,r=e._vModifiers;if(a(r)){if(r.number)return y(n)!==y(t);if(r.trim)return n.trim()!==t.trim()}return n!==t}(e,t))}var Jr={create:Qr,update:Qr},Yr=j((function(e){var t={},n=/:(.+)/;return e.split(/;(?![^(]*\))/g).forEach((function(e){if(e){var r=e.split(n);r.length>1&&(t[r[0].trim()]=r[1].trim())}})),t}));function Zr(e){var t=eo(e.style);return e.staticStyle?A(e.staticStyle,t):t}function eo(e){return Array.isArray(e)?z(e):"string"==typeof e?Yr(e):e}var to,no=/^--/,ro=/\s*!important$/,oo=function(e,t,n){if(no.test(t))e.style.setProperty(t,n);else if(ro.test(n))e.style.setProperty($(t),n.replace(ro,""),"important");else{var r=ao(t);if(Array.isArray(n))for(var o=0,i=n.length;o-1?t.split(uo).forEach((function(t){return e.classList.add(t)})):e.classList.add(t);else{var n=" ".concat(e.getAttribute("class")||""," ");n.indexOf(" "+t+" ")<0&&e.setAttribute("class",(n+t).trim())}}function fo(e,t){if(t&&(t=t.trim()))if(e.classList)t.indexOf(" ")>-1?t.split(uo).forEach((function(t){return e.classList.remove(t)})):e.classList.remove(t),e.classList.length||e.removeAttribute("class");else{for(var n=" ".concat(e.getAttribute("class")||""," "),r=" "+t+" ";n.indexOf(r)>=0;)n=n.replace(r," ");(n=n.trim())?e.setAttribute("class",n):e.removeAttribute("class")}}function po(e){if(e){if("object"==typeof e){var t={};return!1!==e.css&&A(t,ho(e.name||"v")),A(t,e),t}return"string"==typeof e?ho(e):void 0}}var ho=j((function(e){return{enterClass:"".concat(e,"-enter"),enterToClass:"".concat(e,"-enter-to"),enterActiveClass:"".concat(e,"-enter-active"),leaveClass:"".concat(e,"-leave"),leaveToClass:"".concat(e,"-leave-to"),leaveActiveClass:"".concat(e,"-leave-active")}})),vo=Q&&!Y,go="transition",mo="transitionend",yo="animation",bo="animationend";vo&&(void 0===window.ontransitionend&&void 0!==window.onwebkittransitionend&&(go="WebkitTransition",mo="webkitTransitionEnd"),void 0===window.onanimationend&&void 0!==window.onwebkitanimationend&&(yo="WebkitAnimation",bo="webkitAnimationEnd"));var _o=Q?window.requestAnimationFrame?window.requestAnimationFrame.bind(window):setTimeout:function(e){return e()};function xo(e){_o((function(){_o(e)}))}function wo(e,t){var n=e._transitionClasses||(e._transitionClasses=[]);n.indexOf(t)<0&&(n.push(t),co(e,t))}function ko(e,t){e._transitionClasses&&x(e._transitionClasses,t),fo(e,t)}function jo(e,t,n){var r=Po(e,t),o=r.type,i=r.timeout,a=r.propCount;if(!o)return n();var s="transition"===o?mo:bo,l=0,u=function(){e.removeEventListener(s,c),n()},c=function(t){t.target===e&&++l>=a&&u()};setTimeout((function(){l0&&(n="transition",c=a,f=i.length):"animation"===t?u>0&&(n="animation",c=u,f=l.length):f=(n=(c=Math.max(a,u))>0?a>u?"transition":"animation":null)?"transition"===n?i.length:l.length:0,{type:n,timeout:c,propCount:f,hasTransform:"transition"===n&&Co.test(r[go+"Property"])}}function So(e,t){for(;e.length1}function zo(e,t){!0!==t.data.show&&$o(t)}var Lo=function(e){var t,n,r={},u=e.modules,c=e.nodeOps;for(t=0;td?_(e,i(n[m+1])?null:n[m+1].elm,n,h,m,r):h>m&&w(t,f,d)}(f,v,m,n,u):a(m)?(a(e.text)&&c.setTextContent(f,""),_(f,null,m,0,m.length-1,n)):a(v)?w(v,0,v.length-1):a(e.text)&&c.setTextContent(f,""):e.text!==t.text&&c.setTextContent(f,t.text),a(d)&&a(h=d.hook)&&a(h=h.postpatch)&&h(e,t)}}}function P(e,t,n){if(s(n)&&a(e.parent))e.parent.data.pendingInsert=t;else for(var r=0;r-1,a.selected!==i&&(a.selected=i);else if(M(qo(a),r))return void(e.selectedIndex!==s&&(e.selectedIndex=s));o||(e.selectedIndex=-1)}}function Do(e,t){return t.every((function(t){return!M(t,e)}))}function qo(e){return"_value"in e?e._value:e.value}function Go(e){e.target.composing=!0}function No(e){e.target.composing&&(e.target.composing=!1,Fo(e.target,"input"))}function Fo(e,t){var n=document.createEvent("HTMLEvents");n.initEvent(t,!0,!0),e.dispatchEvent(n)}function Uo(e){return!e.componentInstance||e.data&&e.data.transition?e:Uo(e.componentInstance._vnode)}var Bo={model:Io,show:{bind:function(e,t,n){var r=t.value,o=(n=Uo(n)).data&&n.data.transition,i=e.__vOriginalDisplay="none"===e.style.display?"":e.style.display;r&&o?(n.data.show=!0,$o(n,(function(){e.style.display=i}))):e.style.display=r?i:"none"},update:function(e,t,n){var r=t.value;!r!=!t.oldValue&&((n=Uo(n)).data&&n.data.transition?(n.data.show=!0,r?$o(n,(function(){e.style.display=e.__vOriginalDisplay})):Eo(n,(function(){e.style.display="none"}))):e.style.display=r?e.__vOriginalDisplay:"none")},unbind:function(e,t,n,r,o){o||(e.style.display=e.__vOriginalDisplay)}}},Ho={name:String,appear:Boolean,css:Boolean,mode:String,type:String,enterClass:String,leaveClass:String,enterToClass:String,leaveToClass:String,enterActiveClass:String,leaveActiveClass:String,appearClass:String,appearActiveClass:String,appearToClass:String,duration:[Number,String,Object]};function Vo(e){var t=e&&e.componentOptions;return t&&t.Ctor.options.abstract?Vo(jt(t.children)):e}function Wo(e){var t={},n=e.$options;for(var r in n.propsData)t[r]=e[r];var o=n._parentListeners;for(var r in o)t[P(r)]=o[r];return t}function Ko(e,t){if(/\d-keep-alive$/.test(t.tag))return e("keep-alive",{props:t.componentOptions.propsData})}var Qo=function(e){return e.tag||dt(e)},Xo=function(e){return"show"===e.name},Jo={name:"transition",props:Ho,abstract:!0,render:function(e){var t=this,n=this.$slots.default;if(n&&(n=n.filter(Qo)).length){0;var r=this.mode;0;var o=n[0];if(function(e){for(;e=e.parent;)if(e.data.transition)return!0}(this.$vnode))return o;var i=Vo(o);if(!i)return o;if(this._leaving)return Ko(e,o);var a="__transition-".concat(this._uid,"-");i.key=null==i.key?i.isComment?a+"comment":a+i.tag:l(i.key)?0===String(i.key).indexOf(a)?i.key:a+i.key:i.key;var s=(i.data||(i.data={})).transition=Wo(this),u=this._vnode,c=Vo(u);if(i.data.directives&&i.data.directives.some(Xo)&&(i.data.show=!0),c&&c.data&&!function(e,t){return t.key===e.key&&t.tag===e.tag}(i,c)&&!dt(c)&&(!c.componentInstance||!c.componentInstance._vnode.isComment)){var f=c.data.transition=A({},s);if("out-in"===r)return this._leaving=!0,We(f,"afterLeave",(function(){t._leaving=!1,t.$forceUpdate()})),Ko(e,o);if("in-out"===r){if(dt(i))return u;var p,h=function(){p()};We(s,"afterEnter",h),We(s,"enterCancelled",h),We(f,"delayLeave",(function(e){p=e}))}}return o}}},Yo=A({tag:String,moveClass:String},Ho);function Zo(e){e.elm._moveCb&&e.elm._moveCb(),e.elm._enterCb&&e.elm._enterCb()}function ei(e){e.data.newPos=e.elm.getBoundingClientRect()}function ti(e){var t=e.data.pos,n=e.data.newPos,r=t.left-n.left,o=t.top-n.top;if(r||o){e.data.moved=!0;var i=e.elm.style;i.transform=i.WebkitTransform="translate(".concat(r,"px,").concat(o,"px)"),i.transitionDuration="0s"}}delete Yo.mode;var ni={Transition:Jo,TransitionGroup:{props:Yo,beforeMount:function(){var e=this,t=this._update;this._update=function(n,r){var o=Jt(e);e.__patch__(e._vnode,e.kept,!1,!0),e._vnode=e.kept,o(),t.call(e,n,r)}},render:function(e){for(var t=this.tag||this.$vnode.data.tag||"span",n=Object.create(null),r=this.prevChildren=this.children,o=this.$slots.default||[],i=this.children=[],a=Wo(this),s=0;s-1?yr[e]=t.constructor===window.HTMLUnknownElement||t.constructor===window.HTMLElement:yr[e]=/HTMLUnknownElement/.test(t.toString())},A(Wn.options.directives,Bo),A(Wn.options.components,ni),Wn.prototype.__patch__=Q?Lo:L,Wn.prototype.$mount=function(e,t){return function(e,t,n){var r;e.$el=t,e.$options.render||(e.$options.render=de),en(e,"beforeMount"),r=function(){e._update(e._render(),n)},new Ht(e,r,L,{before:function(){e._isMounted&&!e._isDestroyed&&en(e,"beforeUpdate")}},!0),n=!1;var o=e._preWatchers;if(o)for(var i=0;i=0&&(t=e.slice(r),e=e.slice(0,r));var o=e.indexOf("?");return o>=0&&(n=e.slice(o+1),e=e.slice(0,o)),{path:e,query:n,hash:t}}(o.path||""),u=t&&t.path||"/",c=l.path?ki(l.path,u,n||o.append):u,f=function(e,t,n){void 0===t&&(t={});var r,o=n||ci;try{r=o(e||"")}catch(e){r={}}for(var i in t){var a=t[i];r[i]=Array.isArray(a)?a.map(ui):ui(a)}return r}(l.query,o.query,r&&r.options.parseQuery),p=o.hash||l.hash;return p&&"#"!==p.charAt(0)&&(p="#"+p),{_normalized:!0,path:c,query:f,hash:p}}var Bi,Hi=function(){},Vi={name:"RouterLink",props:{to:{type:[String,Object],required:!0},tag:{type:String,default:"a"},custom:Boolean,exact:Boolean,exactPath:Boolean,append:Boolean,replace:Boolean,activeClass:String,exactActiveClass:String,ariaCurrentValue:{type:String,default:"page"},event:{type:[String,Array],default:"click"}},render:function(e){var t=this,n=this.$router,r=this.$route,o=n.resolve(this.to,r,this.append),i=o.location,a=o.route,s=o.href,l={},u=n.options.linkActiveClass,c=n.options.linkExactActiveClass,f=null==u?"router-link-active":u,p=null==c?"router-link-exact-active":c,h=null==this.activeClass?f:this.activeClass,d=null==this.exactActiveClass?p:this.exactActiveClass,v=a.redirectedFrom?hi(null,Ui(a.redirectedFrom),null,n):a;l[d]=yi(r,v,this.exactPath),l[h]=this.exact||this.exactPath?l[d]:function(e,t){return 0===e.path.replace(pi,"/").indexOf(t.path.replace(pi,"/"))&&(!t.hash||e.hash===t.hash)&&function(e,t){for(var n in t)if(!(n in e))return!1;return!0}(e.query,t.query)}(r,v);var g=l[d]?this.ariaCurrentValue:null,m=function(e){Wi(e)&&(t.replace?n.replace(i,Hi):n.push(i,Hi))},y={click:Wi};Array.isArray(this.event)?this.event.forEach((function(e){y[e]=m})):y[this.event]=m;var b={class:l},_=!this.$scopedSlots.$hasNormal&&this.$scopedSlots.default&&this.$scopedSlots.default({href:s,route:a,navigate:m,isActive:l[h],isExactActive:l[d]});if(_){if(1===_.length)return _[0];if(_.length>1||!_.length)return 0===_.length?e():e("span",{},_)}if("a"===this.tag)b.on=y,b.attrs={href:s,"aria-current":g};else{var x=function e(t){var n;if(t)for(var r=0;r-1&&(s.params[p]=n.params[p]);return s.path=Fi(c.path,s.params),l(c,s,a)}if(s.path){s.params={};for(var h=0;h-1}function ja(e,t){return ka(e)&&e._isRouter&&(null==t||e.type===t)}function Ca(e,t,n){var r=function(o){o>=e.length?n():e[o]?t(e[o],(function(){r(o+1)})):r(o+1)};r(0)}function Pa(e){return function(t,n,r){var o=!1,i=0,a=null;Sa(e,(function(e,t,n,s){if("function"==typeof e&&void 0===e.cid){o=!0,i++;var l,u=Ea((function(t){var o;((o=t).__esModule||$a&&"Module"===o[Symbol.toStringTag])&&(t=t.default),e.resolved="function"==typeof t?t:Bi.extend(t),n.components[s]=t,--i<=0&&r()})),c=Ea((function(e){var t="Failed to resolve async component "+s+": "+e;a||(a=ka(e)?e:new Error(t),r(a))}));try{l=e(u,c)}catch(e){c(e)}if(l)if("function"==typeof l.then)l.then(u,c);else{var f=l.component;f&&"function"==typeof f.then&&f.then(u,c)}}})),o||r()}}function Sa(e,t){return Oa(e.map((function(e){return Object.keys(e.components).map((function(n){return t(e.components[n],e.instances[n],e,n)}))})))}function Oa(e){return Array.prototype.concat.apply([],e)}var $a="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag;function Ea(e){var t=!1;return function(){for(var n=[],r=arguments.length;r--;)n[r]=arguments[r];if(!t)return t=!0,e.apply(this,n)}}var Ta=function(e,t){this.router=e,this.base=function(e){if(!e)if(Ki){var t=document.querySelector("base");e=(e=t&&t.getAttribute("href")||"/").replace(/^https?:\/\/[^\/]+/,"")}else e="/";"/"!==e.charAt(0)&&(e="/"+e);return e.replace(/\/$/,"")}(t),this.current=vi,this.pending=null,this.ready=!1,this.readyCbs=[],this.readyErrorCbs=[],this.errorCbs=[],this.listeners=[]};function Aa(e,t,n,r){var o=Sa(e,(function(e,r,o,i){var a=function(e,t){"function"!=typeof e&&(e=Bi.extend(e));return e.options[t]}(e,t);if(a)return Array.isArray(a)?a.map((function(e){return n(e,r,o,i)})):n(a,r,o,i)}));return Oa(r?o.reverse():o)}function za(e,t){if(t)return function(){return e.apply(t,arguments)}}Ta.prototype.listen=function(e){this.cb=e},Ta.prototype.onReady=function(e,t){this.ready?e():(this.readyCbs.push(e),t&&this.readyErrorCbs.push(t))},Ta.prototype.onError=function(e){this.errorCbs.push(e)},Ta.prototype.transitionTo=function(e,t,n){var r,o=this;try{r=this.router.match(e,this.current)}catch(e){throw this.errorCbs.forEach((function(t){t(e)})),e}var i=this.current;this.confirmTransition(r,(function(){o.updateRoute(r),t&&t(r),o.ensureURL(),o.router.afterHooks.forEach((function(e){e&&e(r,i)})),o.ready||(o.ready=!0,o.readyCbs.forEach((function(e){e(r)})))}),(function(e){n&&n(e),e&&!o.ready&&(ja(e,ya.redirected)&&i===vi||(o.ready=!0,o.readyErrorCbs.forEach((function(t){t(e)}))))}))},Ta.prototype.confirmTransition=function(e,t,n){var r=this,o=this.current;this.pending=e;var i,a,s=function(e){!ja(e)&&ka(e)&&(r.errorCbs.length?r.errorCbs.forEach((function(t){t(e)})):console.error(e)),n&&n(e)},l=e.matched.length-1,u=o.matched.length-1;if(yi(e,o)&&l===u&&e.matched[l]===o.matched[u])return this.ensureURL(),e.hash&&aa(this.router,o,e,!1),s(((a=xa(i=o,e,ya.duplicated,'Avoided redundant navigation to current location: "'+i.fullPath+'".')).name="NavigationDuplicated",a));var c=function(e,t){var n,r=Math.max(e.length,t.length);for(n=0;n0)){var t=this.router,n=t.options.scrollBehavior,r=va&&n;r&&this.listeners.push(ia());var o=function(){var n=e.current,o=Ia(e.base);e.current===vi&&o===e._startLocation||e.transitionTo(o,(function(e){r&&aa(t,e,n,!0)}))};window.addEventListener("popstate",o),this.listeners.push((function(){window.removeEventListener("popstate",o)}))}},t.prototype.go=function(e){window.history.go(e)},t.prototype.push=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){ga(ji(r.base+e.fullPath)),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){ma(ji(r.base+e.fullPath)),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.ensureURL=function(e){if(Ia(this.base)!==this.current.fullPath){var t=ji(this.base+this.current.fullPath);e?ga(t):ma(t)}},t.prototype.getCurrentLocation=function(){return Ia(this.base)},t}(Ta);function Ia(e){var t=window.location.pathname,n=t.toLowerCase(),r=e.toLowerCase();return!e||n!==r&&0!==n.indexOf(ji(r+"/"))||(t=t.slice(e.length)),(t||"/")+window.location.search+window.location.hash}var Ra=function(e){function t(t,n,r){e.call(this,t,n),r&&function(e){var t=Ia(e);if(!/^\/#/.test(t))return window.location.replace(ji(e+"/#"+t)),!0}(this.base)||Ma()}return e&&(t.__proto__=e),t.prototype=Object.create(e&&e.prototype),t.prototype.constructor=t,t.prototype.setupListeners=function(){var e=this;if(!(this.listeners.length>0)){var t=this.router.options.scrollBehavior,n=va&&t;n&&this.listeners.push(ia());var r=function(){var t=e.current;Ma()&&e.transitionTo(Da(),(function(r){n&&aa(e.router,r,t,!0),va||Na(r.fullPath)}))},o=va?"popstate":"hashchange";window.addEventListener(o,r),this.listeners.push((function(){window.removeEventListener(o,r)}))}},t.prototype.push=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){Ga(e.fullPath),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){Na(e.fullPath),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.go=function(e){window.history.go(e)},t.prototype.ensureURL=function(e){var t=this.current.fullPath;Da()!==t&&(e?Ga(t):Na(t))},t.prototype.getCurrentLocation=function(){return Da()},t}(Ta);function Ma(){var e=Da();return"/"===e.charAt(0)||(Na("/"+e),!1)}function Da(){var e=window.location.href,t=e.indexOf("#");return t<0?"":e=e.slice(t+1)}function qa(e){var t=window.location.href,n=t.indexOf("#");return(n>=0?t.slice(0,n):t)+"#"+e}function Ga(e){va?ga(qa(e)):window.location.hash=e}function Na(e){va?ma(qa(e)):window.location.replace(qa(e))}var Fa=function(e){function t(t,n){e.call(this,t,n),this.stack=[],this.index=-1}return e&&(t.__proto__=e),t.prototype=Object.create(e&&e.prototype),t.prototype.constructor=t,t.prototype.push=function(e,t,n){var r=this;this.transitionTo(e,(function(e){r.stack=r.stack.slice(0,r.index+1).concat(e),r.index++,t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this;this.transitionTo(e,(function(e){r.stack=r.stack.slice(0,r.index).concat(e),t&&t(e)}),n)},t.prototype.go=function(e){var t=this,n=this.index+e;if(!(n<0||n>=this.stack.length)){var r=this.stack[n];this.confirmTransition(r,(function(){var e=t.current;t.index=n,t.updateRoute(r),t.router.afterHooks.forEach((function(t){t&&t(r,e)}))}),(function(e){ja(e,ya.duplicated)&&(t.index=n)}))}},t.prototype.getCurrentLocation=function(){var e=this.stack[this.stack.length-1];return e?e.fullPath:"/"},t.prototype.ensureURL=function(){},t}(Ta),Ua=function(e){void 0===e&&(e={}),this.app=null,this.apps=[],this.options=e,this.beforeHooks=[],this.resolveHooks=[],this.afterHooks=[],this.matcher=Ji(e.routes||[],this);var t=e.mode||"hash";switch(this.fallback="history"===t&&!va&&!1!==e.fallback,this.fallback&&(t="hash"),Ki||(t="abstract"),this.mode=t,t){case"history":this.history=new La(this,e.base);break;case"hash":this.history=new Ra(this,e.base,this.fallback);break;case"abstract":this.history=new Fa(this,e.base);break;default:0}},Ba={currentRoute:{configurable:!0}};Ua.prototype.match=function(e,t,n){return this.matcher.match(e,t,n)},Ba.currentRoute.get=function(){return this.history&&this.history.current},Ua.prototype.init=function(e){var t=this;if(this.apps.push(e),e.$once("hook:destroyed",(function(){var n=t.apps.indexOf(e);n>-1&&t.apps.splice(n,1),t.app===e&&(t.app=t.apps[0]||null),t.app||t.history.teardown()})),!this.app){this.app=e;var n=this.history;if(n instanceof La||n instanceof Ra){var r=function(e){n.setupListeners(),function(e){var r=n.current,o=t.options.scrollBehavior;va&&o&&"fullPath"in e&&aa(t,e,r,!1)}(e)};n.transitionTo(n.getCurrentLocation(),r,r)}n.listen((function(e){t.apps.forEach((function(t){t._route=e}))}))}},Ua.prototype.beforeEach=function(e){return Va(this.beforeHooks,e)},Ua.prototype.beforeResolve=function(e){return Va(this.resolveHooks,e)},Ua.prototype.afterEach=function(e){return Va(this.afterHooks,e)},Ua.prototype.onReady=function(e,t){this.history.onReady(e,t)},Ua.prototype.onError=function(e){this.history.onError(e)},Ua.prototype.push=function(e,t,n){var r=this;if(!t&&!n&&"undefined"!=typeof Promise)return new Promise((function(t,n){r.history.push(e,t,n)}));this.history.push(e,t,n)},Ua.prototype.replace=function(e,t,n){var r=this;if(!t&&!n&&"undefined"!=typeof Promise)return new Promise((function(t,n){r.history.replace(e,t,n)}));this.history.replace(e,t,n)},Ua.prototype.go=function(e){this.history.go(e)},Ua.prototype.back=function(){this.go(-1)},Ua.prototype.forward=function(){this.go(1)},Ua.prototype.getMatchedComponents=function(e){var t=e?e.matched?e:this.resolve(e).route:this.currentRoute;return t?[].concat.apply([],t.matched.map((function(e){return Object.keys(e.components).map((function(t){return e.components[t]}))}))):[]},Ua.prototype.resolve=function(e,t,n){var r=Ui(e,t=t||this.history.current,n,this),o=this.match(r,t),i=o.redirectedFrom||o.fullPath;return{location:r,route:o,href:function(e,t,n){var r="hash"===n?"#"+t:t;return e?ji(e+"/"+r):r}(this.history.base,i,this.mode),normalizedTo:r,resolved:o}},Ua.prototype.getRoutes=function(){return this.matcher.getRoutes()},Ua.prototype.addRoute=function(e,t){this.matcher.addRoute(e,t),this.history.current!==vi&&this.history.transitionTo(this.history.getCurrentLocation())},Ua.prototype.addRoutes=function(e){this.matcher.addRoutes(e),this.history.current!==vi&&this.history.transitionTo(this.history.getCurrentLocation())},Object.defineProperties(Ua.prototype,Ba);var Ha=Ua;function Va(e,t){return e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}Ua.install=function e(t){if(!e.installed||Bi!==t){e.installed=!0,Bi=t;var n=function(e){return void 0!==e},r=function(e,t){var r=e.$options._parentVnode;n(r)&&n(r=r.data)&&n(r=r.registerRouteInstance)&&r(e,t)};t.mixin({beforeCreate:function(){n(this.$options.router)?(this._routerRoot=this,this._router=this.$options.router,this._router.init(this),t.util.defineReactive(this,"_route",this._router.history.current)):this._routerRoot=this.$parent&&this.$parent._routerRoot||this,r(this,this)},destroyed:function(){r(this)}}),Object.defineProperty(t.prototype,"$router",{get:function(){return this._routerRoot._router}}),Object.defineProperty(t.prototype,"$route",{get:function(){return this._routerRoot._route}}),t.component("RouterView",xi),t.component("RouterLink",Vi);var o=t.config.optionMergeStrategies;o.beforeRouteEnter=o.beforeRouteLeave=o.beforeRouteUpdate=o.created}},Ua.version="3.6.5",Ua.isNavigationFailure=ja,Ua.NavigationFailureType=ya,Ua.START_LOCATION=vi,Ki&&window.Vue&&window.Vue.use(Ua);n(98);n(125),n(91);var Wa={"components/AlgoliaSearchBox":()=>Promise.all([n.e(0),n.e(13)]).then(n.bind(null,307)),"components/DropdownLink":()=>Promise.all([n.e(0),n.e(14)]).then(n.bind(null,259)),"components/DropdownTransition":()=>Promise.all([n.e(0),n.e(21)]).then(n.bind(null,245)),"components/Home":()=>Promise.all([n.e(0),n.e(16)]).then(n.bind(null,288)),"components/NavLink":()=>n.e(27).then(n.bind(null,244)),"components/NavLinks":()=>Promise.all([n.e(0),n.e(12)]).then(n.bind(null,270)),"components/Navbar":()=>Promise.all([n.e(0),n.e(1)]).then(n.bind(null,303)),"components/Page":()=>Promise.all([n.e(0),n.e(11)]).then(n.bind(null,289)),"components/PageEdit":()=>Promise.all([n.e(0),n.e(17)]).then(n.bind(null,272)),"components/PageNav":()=>Promise.all([n.e(0),n.e(15)]).then(n.bind(null,273)),"components/Sidebar":()=>Promise.all([n.e(0),n.e(10)]).then(n.bind(null,290)),"components/SidebarButton":()=>Promise.all([n.e(0),n.e(22)]).then(n.bind(null,291)),"components/SidebarGroup":()=>Promise.all([n.e(0),n.e(3)]).then(n.bind(null,271)),"components/SidebarLink":()=>Promise.all([n.e(0),n.e(18)]).then(n.bind(null,260)),"components/SidebarLinks":()=>Promise.all([n.e(0),n.e(3)]).then(n.bind(null,258)),"global-components/Badge":()=>Promise.all([n.e(0),n.e(4)]).then(n.bind(null,312)),"global-components/CodeBlock":()=>Promise.all([n.e(0),n.e(5)]).then(n.bind(null,308)),"global-components/CodeGroup":()=>Promise.all([n.e(0),n.e(6)]).then(n.bind(null,309)),"layouts/404":()=>n.e(7).then(n.bind(null,310)),"layouts/Layout":()=>Promise.all([n.e(0),n.e(1),n.e(2)]).then(n.bind(null,311)),NotFound:()=>n.e(7).then(n.bind(null,310)),Layout:()=>Promise.all([n.e(0),n.e(1),n.e(2)]).then(n.bind(null,311))},Ka={"v-04e5fff4":()=>n.e(29).then(n.bind(null,313)),"v-4fc17f5a":()=>n.e(31).then(n.bind(null,314)),"v-69ce29c7":()=>n.e(32).then(n.bind(null,315)),"v-c546ebae":()=>n.e(33).then(n.bind(null,316)),"v-9e172538":()=>n.e(30).then(n.bind(null,317)),"v-a6bb140e":()=>n.e(34).then(n.bind(null,318)),"v-41e35097":()=>n.e(19).then(n.bind(null,319)),"v-000e4fc7":()=>n.e(35).then(n.bind(null,320)),"v-388cef95":()=>n.e(37).then(n.bind(null,321)),"v-8d322532":()=>n.e(23).then(n.bind(null,322)),"v-37bab107":()=>n.e(36).then(n.bind(null,323)),"v-172539b2":()=>n.e(38).then(n.bind(null,324)),"v-b433d65a":()=>n.e(39).then(n.bind(null,325)),"v-30ff3e89":()=>n.e(41).then(n.bind(null,326)),"v-0fa3efe4":()=>n.e(42).then(n.bind(null,327)),"v-66982838":()=>n.e(44).then(n.bind(null,328)),"v-de7fe9d6":()=>n.e(43).then(n.bind(null,329)),"v-64349095":()=>n.e(45).then(n.bind(null,330)),"v-e4b26f16":()=>n.e(46).then(n.bind(null,331)),"v-52076f24":()=>n.e(47).then(n.bind(null,332)),"v-adba83b2":()=>n.e(40).then(n.bind(null,333)),"v-444dc367":()=>n.e(28).then(n.bind(null,334)),"v-46a3f904":()=>n.e(50).then(n.bind(null,335)),"v-a95f94f2":()=>n.e(24).then(n.bind(null,336)),"v-30329847":()=>n.e(51).then(n.bind(null,337)),"v-59b78160":()=>n.e(49).then(n.bind(null,338)),"v-9a95a632":()=>n.e(52).then(n.bind(null,339)),"v-59bfc02b":()=>n.e(54).then(n.bind(null,340)),"v-648f41b8":()=>n.e(48).then(n.bind(null,341)),"v-4700bfe7":()=>n.e(53).then(n.bind(null,342)),"v-d6a675f2":()=>n.e(55).then(n.bind(null,343)),"v-123e70c3":()=>n.e(57).then(n.bind(null,344)),"v-7c7b70db":()=>n.e(56).then(n.bind(null,345)),"v-9162d5b2":()=>n.e(58).then(n.bind(null,346)),"v-2e51fb11":()=>n.e(25).then(n.bind(null,347)),"v-6a7c97c5":()=>n.e(59).then(n.bind(null,348)),"v-3a551ac7":()=>n.e(61).then(n.bind(null,349)),"v-71628cc5":()=>n.e(62).then(n.bind(null,350)),"v-36b8ee32":()=>n.e(63).then(n.bind(null,351)),"v-e8628a72":()=>n.e(20).then(n.bind(null,352)),"v-0f448959":()=>n.e(65).then(n.bind(null,353)),"v-3f115d44":()=>n.e(64).then(n.bind(null,354)),"v-bce8af32":()=>n.e(66).then(n.bind(null,355)),"v-7e64f407":()=>n.e(68).then(n.bind(null,356)),"v-4964c3a7":()=>n.e(67).then(n.bind(null,357)),"v-7f6e5700":()=>n.e(69).then(n.bind(null,358)),"v-e844302a":()=>n.e(26).then(n.bind(null,359)),"v-347d4327":()=>n.e(70).then(n.bind(null,360)),"v-555fc7c7":()=>n.e(60).then(n.bind(null,361)),"v-40feb13a":()=>n.e(73).then(n.bind(null,362)),"v-735b4bc7":()=>n.e(72).then(n.bind(null,363)),"v-3c133eb2":()=>n.e(71).then(n.bind(null,364))};function Qa(e){const t=Object.create(null);return function(n){return t[n]||(t[n]=e(n))}}const Xa=/-(\w)/g,Ja=Qa(e=>e.replace(Xa,(e,t)=>t?t.toUpperCase():"")),Ya=/\B([A-Z])/g,Za=Qa(e=>e.replace(Ya,"-$1").toLowerCase()),es=Qa(e=>e.charAt(0).toUpperCase()+e.slice(1));function ts(e,t){if(!t)return;if(e(t))return e(t);return t.includes("-")?e(es(Ja(t))):e(es(t))||e(Za(t))}const ns=Object.assign({},Wa,Ka),rs=e=>ns[e],os=e=>Ka[e],is=e=>Wa[e],as=e=>Wn.component(e);function ss(e){return ts(os,e)}function ls(e){return ts(is,e)}function us(e){return ts(rs,e)}function cs(e){return ts(as,e)}function fs(...e){return Promise.all(e.filter(e=>e).map(async e=>{if(!cs(e)&&us(e)){const t=await us(e)();Wn.component(e,t.default)}}))}function ps(e,t){"undefined"!=typeof window&&window.__VUEPRESS__&&(window.__VUEPRESS__[e]=t)}var hs=n(88),ds=n.n(hs),vs=n(89),gs=n.n(vs),ms={created(){if(this.siteMeta=this.$site.headTags.filter(([e])=>"meta"===e).map(([e,t])=>t),this.$ssrContext){const t=this.getMergedMetaTags();this.$ssrContext.title=this.$title,this.$ssrContext.lang=this.$lang,this.$ssrContext.pageMeta=(e=t)?e.map(e=>{let t="{t+=` ${n}="${gs()(e[n])}"`}),t+">"}).join("\n "):"",this.$ssrContext.canonicalLink=bs(this.$canonicalUrl)}var e},mounted(){this.currentMetaTags=[...document.querySelectorAll("meta")],this.updateMeta(),this.updateCanonicalLink()},methods:{updateMeta(){document.title=this.$title,document.documentElement.lang=this.$lang;const e=this.getMergedMetaTags();this.currentMetaTags=_s(e,this.currentMetaTags)},getMergedMetaTags(){const e=this.$page.frontmatter.meta||[];return ds()([{name:"description",content:this.$description}],e,this.siteMeta,xs)},updateCanonicalLink(){ys(),this.$canonicalUrl&&document.head.insertAdjacentHTML("beforeend",bs(this.$canonicalUrl))}},watch:{$page(){this.updateMeta(),this.updateCanonicalLink()}},beforeDestroy(){_s(null,this.currentMetaTags),ys()}};function ys(){const e=document.querySelector("link[rel='canonical']");e&&e.remove()}function bs(e=""){return e?``:""}function _s(e,t){if(t&&[...t].filter(e=>e.parentNode===document.head).forEach(e=>document.head.removeChild(e)),e)return e.map(e=>{const t=document.createElement("meta");return Object.keys(e).forEach(n=>{t.setAttribute(n,e[n])}),document.head.appendChild(t),t})}function xs(e){for(const t of["name","property","itemprop"])if(e.hasOwnProperty(t))return e[t]+t;return JSON.stringify(e)}var ws=n(90),ks={mounted(){window.addEventListener("scroll",this.onScroll)},methods:{onScroll:n.n(ws)()((function(){this.setActiveHash()}),300),setActiveHash(){const e=[].slice.call(document.querySelectorAll(".sidebar-link")),t=[].slice.call(document.querySelectorAll(".header-anchor")).filter(t=>e.some(e=>e.hash===t.hash)),n=Math.max(window.pageYOffset,document.documentElement.scrollTop,document.body.scrollTop),r=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight),o=window.innerHeight+n;for(let e=0;e=i.parentElement.offsetTop+10&&(!a||n{this.$nextTick(()=>{this.$vuepress.$set("disableScrollBehavior",!1)})})}}}},beforeDestroy(){window.removeEventListener("scroll",this.onScroll)}},js=n(22),Cs=n.n(js),Ps=[ms,ks,{mounted(){Cs.a.configure({showSpinner:!1}),this.$router.beforeEach((e,t,n)=>{e.path===t.path||Wn.component(e.name)||Cs.a.start(),n()}),this.$router.afterEach(()=>{Cs.a.done(),this.isSidebarOpen=!1})}}],Ss={name:"GlobalLayout",computed:{layout(){const e=this.getLayout();return ps("layout",e),Wn.component(e)}},methods:{getLayout(){if(this.$page.path){const e=this.$page.frontmatter.layout;return e&&(this.$vuepress.getLayoutAsyncComponent(e)||this.$vuepress.getVueComponent(e))?e:"Layout"}return"NotFound"}}},Os=n(10),$s=Object(Os.a)(Ss,(function(){return(0,this._self._c)(this.layout,{tag:"component"})}),[],!1,null,null,null).exports;!function(e,t,n){switch(t){case"components":e[t]||(e[t]={}),Object.assign(e[t],n);break;case"mixins":e[t]||(e[t]=[]),e[t].push(...n);break;default:throw new Error("Unknown option name.")}}($s,"mixins",Ps);const Es=[{name:"v-04e5fff4",path:"/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-04e5fff4").then(n)}},{path:"/index.html",redirect:"/"},{name:"v-4fc17f5a",path:"/guide/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4fc17f5a").then(n)}},{path:"/guide/index.html",redirect:"/guide/"},{name:"v-69ce29c7",path:"/guide/auto-gen-priest.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-69ce29c7").then(n)}},{name:"v-c546ebae",path:"/guide/config.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-c546ebae").then(n)}},{name:"v-9e172538",path:"/goners/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9e172538").then(n)}},{path:"/goners/index.html",redirect:"/goners/"},{name:"v-a6bb140e",path:"/guide/core-concept.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-a6bb140e").then(n)}},{name:"v-41e35097",path:"/guide/goner-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-41e35097").then(n)}},{name:"v-000e4fc7",path:"/guide/hooks.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-000e4fc7").then(n)}},{name:"v-388cef95",path:"/guide/logrus.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-388cef95").then(n)}},{name:"v-8d322532",path:"/guide/gone-and-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-8d322532").then(n)}},{name:"v-37bab107",path:"/guide/inner-goner.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-37bab107").then(n)}},{name:"v-172539b2",path:"/guide/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-172539b2").then(n)}},{name:"v-b433d65a",path:"/guide/tracer.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-b433d65a").then(n)}},{name:"v-30ff3e89",path:"/guide/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-30ff3e89").then(n)}},{name:"v-0fa3efe4",path:"/quick-start/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-0fa3efe4").then(n)}},{path:"/quick-start/index.html",redirect:"/quick-start/"},{name:"v-66982838",path:"/references/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-66982838").then(n)}},{path:"/references/index.html",redirect:"/references/"},{name:"v-de7fe9d6",path:"/quick-start/simple.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-de7fe9d6").then(n)}},{name:"v-64349095",path:"/references/gone-tool.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-64349095").then(n)}},{name:"v-e4b26f16",path:"/references/http-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e4b26f16").then(n)}},{name:"v-52076f24",path:"/zh/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-52076f24").then(n)}},{path:"/zh/index.html",redirect:"/zh/"},{name:"v-adba83b2",path:"/guide/unit-test.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-adba83b2").then(n)}},{name:"v-444dc367",path:"/zh/blogs/1_v1.x_release.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-444dc367").then(n)}},{name:"v-46a3f904",path:"/zh/guide/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-46a3f904").then(n)}},{path:"/zh/guide/index.html",redirect:"/zh/guide/"},{name:"v-a95f94f2",path:"/quick-start/mysql.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-a95f94f2").then(n)}},{name:"v-30329847",path:"/zh/guide/auto-gen-priest.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-30329847").then(n)}},{name:"v-59b78160",path:"/zh/goners/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-59b78160").then(n)}},{path:"/zh/goners/index.html",redirect:"/zh/goners/"},{name:"v-9a95a632",path:"/zh/guide/config.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9a95a632").then(n)}},{name:"v-59bfc02b",path:"/zh/guide/error.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-59bfc02b").then(n)}},{name:"v-648f41b8",path:"/zh/blogs/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-648f41b8").then(n)}},{path:"/zh/blogs/index.html",redirect:"/zh/blogs/"},{name:"v-4700bfe7",path:"/zh/guide/core-concept.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4700bfe7").then(n)}},{name:"v-d6a675f2",path:"/zh/guide/gone-vs-spring.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-d6a675f2").then(n)}},{name:"v-123e70c3",path:"/zh/guide/inner-goner.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-123e70c3").then(n)}},{name:"v-7c7b70db",path:"/zh/guide/hooks.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7c7b70db").then(n)}},{name:"v-9162d5b2",path:"/zh/guide/logrus.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9162d5b2").then(n)}},{name:"v-2e51fb11",path:"/zh/guide/gone-and-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-2e51fb11").then(n)}},{name:"v-6a7c97c5",path:"/zh/guide/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-6a7c97c5").then(n)}},{name:"v-3a551ac7",path:"/zh/guide/tracer.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3a551ac7").then(n)}},{name:"v-71628cc5",path:"/zh/guide/unit-test.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-71628cc5").then(n)}},{name:"v-36b8ee32",path:"/zh/guide/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-36b8ee32").then(n)}},{name:"v-e8628a72",path:"/zh/guide/goner-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e8628a72").then(n)}},{name:"v-0f448959",path:"/zh/quick-start/gpt.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-0f448959").then(n)}},{name:"v-3f115d44",path:"/zh/quick-start/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3f115d44").then(n)}},{path:"/zh/quick-start/index.html",redirect:"/zh/quick-start/"},{name:"v-bce8af32",path:"/zh/quick-start/jwt+mysql+redis+schedule.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-bce8af32").then(n)}},{name:"v-7e64f407",path:"/zh/quick-start/web.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7e64f407").then(n)}},{name:"v-4964c3a7",path:"/zh/quick-start/simple.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4964c3a7").then(n)}},{name:"v-7f6e5700",path:"/zh/references/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7f6e5700").then(n)}},{path:"/zh/references/index.html",redirect:"/zh/references/"},{name:"v-e844302a",path:"/zh/quick-start/mysql.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e844302a").then(n)}},{name:"v-347d4327",path:"/zh/references/gone-tool.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-347d4327").then(n)}},{name:"v-555fc7c7",path:"/zh/guide/schedule.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-555fc7c7").then(n)}},{name:"v-40feb13a",path:"/zh/references/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-40feb13a").then(n)}},{name:"v-735b4bc7",path:"/zh/references/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-735b4bc7").then(n)}},{name:"v-3c133eb2",path:"/zh/references/http-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3c133eb2").then(n)}},{path:"*",component:$s}],Ts={title:"",description:"",base:"/",headTags:[["link",{rel:"icon",href:"/assets/img/logo.png"}],["script",{type:"text/javascript"},"\n if (location.hostname != 'localhost'){\n var _hmt = _hmt || [];\n (function() {\n var hm = document.createElement(\"script\");\n hm.src = \"https://hm.baidu.com/hm.js?ed9e5a48d8eeb5e6b55a0de9cb8f6486\";\n var s = document.getElementsByTagName(\"script\")[0]; \n s.parentNode.insertBefore(hm, s);\n })();\n\n if(location.pathname == '/my-github'){\n location.href='https://github.com/gone-io/gone'\n }\n }\n "]],pages:[{title:"Introduction",frontmatter:{sidebar:"auto",prev:!1,next:"./quick-start/"},regularPath:"/",relativePath:"README.md",key:"v-04e5fff4",path:"/",headers:[{level:2,title:"What is Gone?",slug:"what-is-gone"},{level:2,title:"Web Service",slug:"web-service"},{level:2,title:"Concepts",slug:"concepts"}]},{title:"Development Guide",frontmatter:{sidebar:"auto",prev:"../quick-start/",next:"../references/"},regularPath:"/guide/",relativePath:"guide/README.md",key:"v-4fc17f5a",path:"/guide/",headers:[{level:2,title:"Core Concepts of Gone",slug:"core-concepts-of-gone"},{level:2,title:"What Injection Methods Does Gone Support?",slug:"what-injection-methods-does-gone-support"},{level:2,title:"How to Gracefully Use Built-in Goners?",slug:"how-to-gracefully-use-built-in-goners"},{level:2,title:"Supporting Configuration Files via Built-in Goners",slug:"supporting-configuration-files-via-built-in-goners"},{level:2,title:"Supporting Database Connections via Built-in Goners",slug:"supporting-database-connections-via-built-in-goners"},{level:2,title:"Hook Functions",slug:"hook-functions"},{level:2,title:"Logging Output",slug:"logging-output"},{level:2,title:"Tracing Logs with TraceID",slug:"tracing-logs-with-traceid"},{level:2,title:"Auto-generating Priests",slug:"auto-generating-priests"}]},{title:"Auto-generate Priest",frontmatter:{sidebar:"auto",prev:"./tracer",next:"./redis"},regularPath:"/guide/auto-gen-priest.html",relativePath:"guide/auto-gen-priest.md",key:"v-69ce29c7",path:"/guide/auto-gen-priest.html",headers:[{level:2,title:"Getting Started",slug:"getting-started"},{level:3,title:"1. Install the gone auxiliary tool",slug:"_1-install-the-gone-auxiliary-tool"},{level:3,title:"2. Write the Goner",slug:"_2-write-the-goner"},{level:3,title:"3. Generate code",slug:"_3-generate-code"},{level:2,title:"Best Practices",slug:"best-practices"}]},{title:"Configuring with Built-in Goners Support",frontmatter:{sidebar:"auto",prev:"./inner-goner",next:"./xorm"},regularPath:"/guide/config.html",relativePath:"guide/config.md",key:"v-c546ebae",path:"/guide/config.html",headers:[{level:2,title:"Example",slug:"example"},{level:3,title:"1. Create a mod",slug:"_1-create-a-mod"},{level:3,title:"2. Add a configuration file",slug:"_2-add-a-configuration-file"},{level:3,title:"3. Add code",slug:"_3-add-code"},{level:3,title:"4. Run",slug:"_4-run"},{level:2,title:"Configuration Injection",slug:"configuration-injection"},{level:3,title:"Tag Format Used",slug:"tag-format-used"},{level:3,title:"Supported Data Types for Injection",slug:"supported-data-types-for-injection"},{level:3,title:"Configuration Files",slug:"configuration-files"}]},{title:"Goners",frontmatter:{sidebar:"auto",prev:"../references/",next:"../"},regularPath:"/goners/",relativePath:"goners/README.md",key:"v-9e172538",path:"/goners/",headers:[{level:2,title:"Core-Level Goners",slug:"core-level-goners"},{level:2,title:"Built-in Framework Goners",slug:"built-in-framework-goners"},{level:2,title:"Ecosystem-Level Goners",slug:"ecosystem-level-goners"}]},{title:"Core Concepts of Gone",frontmatter:{sidebar:"auto",prev:!1,next:"./gone-and-inject"},regularPath:"/guide/core-concept.html",relativePath:"guide/core-concept.md",key:"v-a6bb140e",path:"/guide/core-concept.html",headers:[{level:2,title:"Goner",slug:"goner"},{level:3,title:"🔮 Prophet",slug:"🔮-prophet"},{level:3,title:"😇 Angel",slug:"😇-angel"},{level:3,title:"🧛🏻‍♀️ Vampire",slug:"🧛🏻‍♀️-vampire"},{level:2,title:"Cemetery",slug:"cemetery"},{level:3,title:"Burying",slug:"burying"},{level:3,title:"Reviving",slug:"reviving"},{level:2,title:"Heaven",slug:"heaven"},{level:2,title:"Priest",slug:"priest"}]},{title:"Injection Methods Supported by Gone",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./inner-goner"},regularPath:"/guide/goner-inject.html",relativePath:"guide/goner-inject.md",key:"v-41e35097",path:"/guide/goner-inject.html",headers:[{level:2,title:"Public Property Injection vs Private Property Injection",slug:"public-property-injection-vs-private-property-injection"},{level:2,title:"Anonymous Injection vs Named Injection",slug:"anonymous-injection-vs-named-injection"},{level:3,title:"Anonymous Burial and Named Burial",slug:"anonymous-burial-and-named-burial"},{level:3,title:"Anonymous Injection",slug:"anonymous-injection"},{level:3,title:"Named Injection",slug:"named-injection"},{level:2,title:"Supported Injection Property Types",slug:"supported-injection-property-types"},{level:3,title:"1. Pointer Types",slug:"_1-pointer-types"},{level:3,title:"2. Value Types",slug:"_2-value-types"},{level:3,title:"3. Interface Types",slug:"_3-interface-types"},{level:3,title:"4. Slice Types",slug:"_4-slice-types"},{level:3,title:"5. Map Types",slug:"_5-map-types"},{level:2,title:"Pointer Injection vs Interface Injection",slug:"pointer-injection-vs-interface-injection"},{level:2,title:"Slice Injection vs Map Injection",slug:"slice-injection-vs-map-injection"}]},{title:"Gone's Hook Functions",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./logrus"},regularPath:"/guide/hooks.html",relativePath:"guide/hooks.md",key:"v-000e4fc7",path:"/guide/hooks.html",headers:[{level:2,title:"What are Hook Functions?",slug:"what-are-hook-functions"},{level:2,title:"Code Example",slug:"code-example"},{level:3,title:"Rules for Hook Functions",slug:"rules-for-hook-functions"},{level:2,title:"Gone's Startup Process",slug:"gone-s-startup-process"}]},{title:"Logging Output",frontmatter:{sidebar:"auto",prev:"./hooks",next:"./tracer"},regularPath:"/guide/logrus.html",relativePath:"guide/logrus.md",key:"v-388cef95",path:"/guide/logrus.html",headers:[{level:2,title:"Burying Related Goners in the Cemetery",slug:"burying-related-goners-in-the-cemetery"},{level:2,title:"Adding Related Configurations in the Configuration File",slug:"adding-related-configurations-in-the-configuration-file"},{level:2,title:"Logging",slug:"logging"},{level:2,title:"Log Format",slug:"log-format"},{level:2,title:"About TraceId",slug:"about-traceid"}]},{title:"Goner and Dependency Injection",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./goner-inject"},regularPath:"/guide/gone-and-inject.html",relativePath:"guide/gone-and-inject.md",key:"v-8d322532",path:"/guide/gone-and-inject.html",headers:[{level:2,title:"Goner and Dependency Injection",slug:"goner-and-dependency-injection"},{level:3,title:"Definition of Goner",slug:"definition-of-goner"},{level:3,title:"How Dependency Injection is Achieved in Gone",slug:"how-dependency-injection-is-achieved-in-gone"},{level:3,title:"How to Execute Business Code in Goners?",slug:"how-to-execute-business-code-in-goners"},{level:3,title:"The gone Command: Automatically Generating Priest Functions",slug:"the-gone-command-automatically-generating-priest-functions"},{level:2,title:"Anonymous Injection vs Named Injection",slug:"anonymous-injection-vs-named-injection"},{level:3,title:"Named Burial",slug:"named-burial"},{level:2,title:"Pointer Injection, Value Injection, and Interface Injection",slug:"pointer-injection-value-injection-and-interface-injection"},{level:3,title:"Pointer Injection vs Interface Injection",slug:"pointer-injection-vs-interface-injection"},{level:2,title:"Slice Injection and Map Injection",slug:"slice-injection-and-map-injection"},{level:3,title:"Private Property Injection",slug:"private-property-injection"},{level:2,title:"Configuration Injection",slug:"configuration-injection"}]},{title:"How to Gracefully Use Built-in Goners?",frontmatter:{sidebar:"auto",prev:"./goner-inject",next:"./config"},regularPath:"/guide/inner-goner.html",relativePath:"guide/inner-goner.md",key:"v-37bab107",path:"/guide/inner-goner.html",headers:[{level:2,title:"Built-in Goners",slug:"built-in-goners"},{level:2,title:"The Priest Function",slug:"the-priest-function"},{level:2,title:"How to Use",slug:"how-to-use"},{level:2,title:"GoneId for Built-in Goners",slug:"goneid-for-built-in-goners"}]},{title:"Using Redis for Distributed Locking and Caching",frontmatter:{sidebar:"auto",prev:"./auto-gen-priest",next:"./unit-test"},regularPath:"/guide/redis.html",relativePath:"guide/redis.md",key:"v-172539b2",path:"/guide/redis.html",headers:[{level:2,title:"Step 1: Embedding Redis Related Goner into Cemetery",slug:"step-1-embedding-redis-related-goner-into-cemetery"},{level:2,title:"Step 2: Adding Redis Configuration to the Configuration File",slug:"step-2-adding-redis-configuration-to-the-configuration-file"},{level:2,title:"Step 3: Using Redis",slug:"step-3-using-redis"},{level:3,title:"Injecting Interfaces",slug:"injecting-interfaces"},{level:3,title:"Using Distributed Cache",slug:"using-distributed-cache"},{level:3,title:"Using Distributed Lock",slug:"using-distributed-lock"},{level:2,title:"Complete Example Code",slug:"complete-example-code"}]},{title:"Trace Log with traceId",frontmatter:{sidebar:"auto",prev:"./logrus",next:"./auto-gen-priest"},regularPath:"/guide/tracer.html",relativePath:"guide/tracer.md",key:"v-b433d65a",path:"/guide/tracer.html",headers:[{level:2,title:"Burying Related Goners in the Cemetery",slug:"burying-related-goners-in-the-cemetery"},{level:2,title:"Simple Usage",slug:"simple-usage"},{level:2,title:"Passing TraceId Across Goroutines",slug:"passing-traceid-across-goroutines"},{level:2,title:"Passing TraceId Across Processes/Services",slug:"passing-traceid-across-processes-services"},{level:2,title:"Multi-language Support",slug:"multi-language-support"}]},{title:"Supporting Database Operations with Built-in Goners",frontmatter:{sidebar:"auto",prev:"./config",next:"./hooks"},regularPath:"/guide/xorm.html",relativePath:"guide/xorm.md",key:"v-30ff3e89",path:"/guide/xorm.html",headers:[{level:2,title:"Example with MySQL",slug:"example-with-mysql"},{level:3,title:"1. Prepare MySQL Service",slug:"_1-prepare-mysql-service"},{level:3,title:"2. Create Module and Configuration",slug:"_2-create-module-and-configuration"},{level:3,title:"3. Write Code",slug:"_3-write-code"},{level:3,title:"4. Run the Code",slug:"_4-run-the-code"},{level:2,title:"Points to Note",slug:"points-to-note"},{level:3,title:"1. github.com/gone-io/gone/goner is a wrapper for xorm.io/xorm",slug:"_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"},{level:3,title:"2. Injection via gone-xorm GonerId",slug:"_2-injection-via-gone-xorm-gonerid"},{level:3,title:"3. Pay Attention to Imported Packages",slug:"_3-pay-attention-to-imported-packages"}]},{title:"Quick Start",frontmatter:{sidebar:"auto",prev:"../",next:"../guide/"},regularPath:"/quick-start/",relativePath:"quick-start/README.md",key:"v-0fa3efe4",path:"/quick-start/",headers:[{level:2,title:"Create a Simple Web Project",slug:"create-a-simple-web-project"},{level:2,title:"Create a Production-Ready Project Using MySQL",slug:"create-a-production-ready-project-using-mysql"},{level:2,title:"More Examples",slug:"more-examples"}]},{title:"Reference Documentation",frontmatter:{sidebar:"auto",prev:"../guide/",next:"../goners/"},regularPath:"/references/",relativePath:"references/README.md",key:"v-66982838",path:"/references/",headers:[{level:2,title:"Gone Tool",slug:"gone-tool"},{level:2,title:"HTTP Injection Instructions",slug:"http-injection-instructions"}]},{title:"Creating a Simple Web Project",frontmatter:{sidebar:"auto",prev:!1,next:"./mysql"},regularPath:"/quick-start/simple.html",relativePath:"quick-start/simple.md",key:"v-de7fe9d6",path:"/quick-start/simple.html",headers:[{level:2,title:"Installing the Gone Utility Tool",slug:"installing-the-gone-utility-tool"},{level:2,title:"Creating a Web Project and Running the Code",slug:"creating-a-web-project-and-running-the-code"},{level:2,title:"Project Structure",slug:"project-structure"},{level:2,title:"Router",slug:"router"},{level:2,title:"Controller",slug:"controller"},{level:2,title:"Service",slug:"service"},{level:2,title:"Use Database",slug:"use-database"}]},{title:"Gone Tool",frontmatter:{sidebar:"auto",prev:!1,next:"./http-inject"},regularPath:"/references/gone-tool.html",relativePath:"references/gone-tool.md",key:"v-64349095",path:"/references/gone-tool.html",headers:[{level:2,title:"Installation",slug:"installation"},{level:2,title:"Usage",slug:"usage"},{level:3,title:"Testing Installation",slug:"testing-installation"},{level:3,title:"Generate Project",slug:"generate-project"},{level:3,title:"Generate Priest Function",slug:"generate-priest-function"}]},{title:"HTTP Injection Instructions",frontmatter:{sidebar:"auto",prev:"./gone-tool",next:!1},regularPath:"/references/http-inject.html",relativePath:"references/http-inject.md",key:"v-e4b26f16",path:"/references/http-inject.html",headers:[{level:2,title:"Format of HTTP Dependency Injection Tags",slug:"format-of-http-dependency-injection-tags"},{level:2,title:"Supported Injection Types and Response Tags",slug:"supported-injection-types-and-response-tags"},{level:2,title:"Notes",slug:"notes"}]},{title:"介绍",frontmatter:{sidebar:"auto",prev:!1,next:"./quick-start/"},regularPath:"/zh/",relativePath:"zh/README.md",key:"v-52076f24",path:"/zh/",headers:[{level:2,title:"Gone是什么",slug:"gone是什么"},{level:2,title:"Web服务",slug:"web服务"},{level:2,title:"概念",slug:"概念"}]},{title:"Unit Testing",frontmatter:{sidebar:"auto",prev:"./redis",next:!1},regularPath:"/guide/unit-test.html",relativePath:"guide/unit-test.md",key:"v-adba83b2",path:"/guide/unit-test.html",headers:[{level:2,title:"Assume Our Written Goner is as Follows",slug:"assume-our-written-goner-is-as-follows"},{level:2,title:"We Can Write a Test File as Follows:",slug:"we-can-write-a-test-file-as-follows"},{level:2,title:"Using gomock for Mock Testing",slug:"using-gomock-for-mock-testing"}]},{title:"v1.x版本 正式发布",frontmatter:{sidebar:"auto",prev:!1,next:!1},regularPath:"/zh/blogs/1_v1.x_release.html",relativePath:"zh/blogs/1_v1.x_release.md",key:"v-444dc367",path:"/zh/blogs/1_v1.x_release.html",headers:[{level:2,title:"优化和新特性",slug:"优化和新特性"},{level:3,title:"gone 核心功能增强",slug:"gone-核心功能增强"},{level:3,title:"内置Goners",slug:"内置goners"},{level:3,title:"覆盖测试",slug:"覆盖测试"},{level:2,title:"后续计划",slug:"后续计划"}]},{title:"开发指南",frontmatter:{sidebar:"auto",prev:"../quick-start/",next:"../references/"},regularPath:"/zh/guide/",relativePath:"zh/guide/README.md",key:"v-46a3f904",path:"/zh/guide/",headers:[{level:2,title:"Gone的核心概念",slug:"gone的核心概念"},{level:2,title:"依赖注入",slug:"依赖注入"},{level:3,title:"goner和依赖注入",slug:"goner和依赖注入"},{level:3,title:"Gone支持哪些方式注入?",slug:"gone支持哪些方式注入"},{level:3,title:"对比 Gone 和 Spring 的依赖注入",slug:"对比-gone-和-spring-的依赖注入"},{level:2,title:"内置模块使用",slug:"内置模块使用"},{level:3,title:"如何优雅使用内置Goners?",slug:"如何优雅使用内置goners"},{level:3,title:"通过内置Goners支持配置文件",slug:"通过内置goners支持配置文件"},{level:3,title:"日志输出",slug:"日志输出"},{level:3,title:"使用traceId追踪日志",slug:"使用traceid追踪日志"},{level:3,title:"通过内置Goners支持数据库连接",slug:"通过内置goners支持数据库连接"},{level:3,title:"利用redis提供分布式锁和分布式缓存",slug:"利用redis提供分布式锁和分布式缓存"},{level:3,title:"用cron表达式配置定时任务",slug:"用cron表达式配置定时任务"},{level:2,title:"效率提升",slug:"效率提升"},{level:3,title:"自动生成Priest",slug:"自动生成priest"},{level:3,title:"Hook函数",slug:"hook函数"},{level:3,title:"错误处理",slug:"错误处理"},{level:3,title:"单元测试",slug:"单元测试"}]},{title:"Web + MySQL",frontmatter:{sidebar:"auto",prev:"./simple",next:!1},regularPath:"/quick-start/mysql.html",relativePath:"quick-start/mysql.md",key:"v-a95f94f2",path:"/quick-start/mysql.html",headers:[{level:2,title:"Install the gone Tool",slug:"install-the-gone-tool"},{level:2,title:"Create a Project Using the gone Command",slug:"create-a-project-using-the-gone-command"},{level:2,title:"Compile And Run",slug:"compile-and-run"},{level:2,title:"API Testing",slug:"api-testing"},{level:2,title:"Project Brief Introduction",slug:"project-brief-introduction"},{level:3,title:"Directory Structure",slug:"directory-structure"},{level:3,title:"Project Features",slug:"project-features"},{level:3,title:"Partial Code Explanation",slug:"partial-code-explanation"},{level:3,title:"HTTP Injection",slug:"http-injection"},{level:3,title:"Request Handling Function Parameters",slug:"request-handling-function-parameters"}]},{title:"自动生成Priest",frontmatter:{sidebar:"auto",prev:"./schedule",next:"./hooks"},regularPath:"/zh/guide/auto-gen-priest.html",relativePath:"zh/guide/auto-gen-priest.md",key:"v-30329847",path:"/zh/guide/auto-gen-priest.html",headers:[{level:2,title:"小试牛刀",slug:"小试牛刀"},{level:3,title:"1. 安装gone辅助工具",slug:"_1-安装gone辅助工具"},{level:3,title:"2. 编写Goner",slug:"_2-编写goner"},{level:3,title:"3. 生成代码",slug:"_3-生成代码"},{level:2,title:"最佳实践",slug:"最佳实践"}]},{title:"Goners",frontmatter:{sidebar:"auto",prev:"../references/",next:"../"},regularPath:"/zh/goners/",relativePath:"zh/goners/README.md",key:"v-59b78160",path:"/zh/goners/",headers:[{level:2,title:"核心级Goners",slug:"核心级goners"},{level:2,title:"框架内置Goners",slug:"框架内置goners"},{level:2,title:"生态级 Goners",slug:"生态级-goners"}]},{title:"通过内置Goners支持配置文件",frontmatter:{sidebar:"auto",prev:"./inner-goner",next:"./logrus"},regularPath:"/zh/guide/config.html",relativePath:"zh/guide/config.md",key:"v-9a95a632",path:"/zh/guide/config.html",headers:[{level:2,title:"举个例子",slug:"举个例子"},{level:3,title:"1. 创建mod",slug:"_1-创建mod"},{level:3,title:"2. 添加配置文件",slug:"_2-添加配置文件"},{level:3,title:"3. 添加代码",slug:"_3-添加代码"},{level:3,title:"4. 运行",slug:"_4-运行"},{level:2,title:"配置注入",slug:"配置注入"},{level:3,title:"使用的标签格式",slug:"使用的标签格式"},{level:3,title:"支持注入的数据类型",slug:"支持注入的数据类型"},{level:3,title:"配置文件",slug:"配置文件"}]},{title:"错误处理",frontmatter:{sidebar:"auto",prev:"./hooks",next:"./unit-test"},regularPath:"/zh/guide/error.html",relativePath:"zh/guide/error.md",key:"v-59bfc02b",path:"/zh/guide/error.html",headers:[{level:2,title:"Gone中定义了三种错误类型:",slug:"gone中定义了三种错误类型"},{level:2,title:"为什么要定义这三种错误?",slug:"为什么要定义这三种错误"},{level:3,title:"服务器错误",slug:"服务器错误"},{level:3,title:"客户端错误",slug:"客户端错误"},{level:3,title:"业务错误",slug:"业务错误"},{level:2,title:"总结",slug:"总结"}]},{title:"博客",frontmatter:{sidebar:"auto",prev:!1,next:"./1_v1.x_release"},regularPath:"/zh/blogs/",relativePath:"zh/blogs/README.md",key:"v-648f41b8",path:"/zh/blogs/",headers:[{level:2,title:"v1.x版本 正式发布",slug:"v1-x版本-正式发布"}]},{title:"Gone的核心概念",frontmatter:{sidebar:"auto",prev:!1,next:"./gone-and-inject"},regularPath:"/zh/guide/core-concept.html",relativePath:"zh/guide/core-concept.md",key:"v-4700bfe7",path:"/zh/guide/core-concept.html",headers:[{level:2,title:"Goner(逝者)",slug:"goner-逝者"},{level:3,title:"🔮Prophet(先知)",slug:"🔮prophet-先知"},{level:3,title:"😇Angel(天使)",slug:"😇angel-天使"},{level:3,title:"🧛🏻‍♀️Vampire(吸血鬼)",slug:"🧛🏻‍♀️vampire-吸血鬼"},{level:2,title:"Cemetery(墓园)",slug:"cemetery-墓园"},{level:3,title:"Bury(埋葬)",slug:"bury-埋葬"},{level:3,title:"Revive(复活)",slug:"revive-复活"},{level:2,title:"Heaven(天国)",slug:"heaven-天国"},{level:2,title:"Priest (牧师)",slug:"priest-牧师"}]},{title:"对比 Gone 和 Spring 的依赖注入",frontmatter:{sidebar:"auto",sidebarDepth:4,prev:"./goner-inject",next:"./inner-goner"},regularPath:"/zh/guide/gone-vs-spring.html",relativePath:"zh/guide/gone-vs-spring.md",key:"v-d6a675f2",path:"/zh/guide/gone-vs-spring.html",headers:[{level:2,title:"依赖模块导入",slug:"依赖模块导入"},{level:3,title:"Spring",slug:"spring"},{level:3,title:"Gone",slug:"gone"},{level:2,title:"注入依赖",slug:"注入依赖"},{level:3,title:"Spring",slug:"spring-2"},{level:3,title:"Goner",slug:"goner"},{level:2,title:"配置注入",slug:"配置注入"},{level:3,title:"Spring",slug:"spring-3"},{level:2,title:"HTTP 参数注入",slug:"http-参数注入"},{level:3,title:"注入Query参数",slug:"注入query参数"},{level:3,title:"注入路径参数",slug:"注入路径参数"},{level:3,title:"请求体注入",slug:"请求体注入"},{level:2,title:"总结",slug:"总结"}]},{title:"如何优雅使用内置Goners?",frontmatter:{sidebar:"auto",prev:"./gone-vs-spring",next:"./config"},regularPath:"/zh/guide/inner-goner.html",relativePath:"zh/guide/inner-goner.md",key:"v-123e70c3",path:"/zh/guide/inner-goner.html",headers:[{level:2,title:"内置Goners",slug:"内置goners"},{level:2,title:"Priest函数",slug:"priest函数"},{level:2,title:"如何使用",slug:"如何使用"},{level:2,title:"内置Goner的GoneId",slug:"内置goner的goneid"}]},{title:"Gone的Hook函数",frontmatter:{sidebar:"auto",prev:"./auto-gen-priest",next:"./error"},regularPath:"/zh/guide/hooks.html",relativePath:"zh/guide/hooks.md",key:"v-7c7b70db",path:"/zh/guide/hooks.html",headers:[{level:2,title:"什么是Hook函数",slug:"什么是hook函数"},{level:2,title:"看一段代码",slug:"看一段代码"},{level:3,title:"Hook函数遵循以下规则",slug:"hook函数遵循以下规则"},{level:2,title:"Gone的启动流程",slug:"gone的启动流程"}]},{title:"日志输出",frontmatter:{sidebar:"auto",prev:"./config",next:"./tracer"},regularPath:"/zh/guide/logrus.html",relativePath:"zh/guide/logrus.md",key:"v-9162d5b2",path:"/zh/guide/logrus.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"在配置文件中添加相关配置",slug:"在配置文件中添加相关配置"},{level:2,title:"打印日志",slug:"打印日志"},{level:2,title:"日志的格式",slug:"日志的格式"},{level:2,title:"关于TraceId",slug:"关于traceid"}]},{title:"Goner和依赖注入",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./goner-inject"},regularPath:"/zh/guide/gone-and-inject.html",relativePath:"zh/guide/gone-and-inject.md",key:"v-2e51fb11",path:"/zh/guide/gone-and-inject.html",headers:[{level:2,title:"Goner和依赖注入",slug:"goner和依赖注入"},{level:3,title:"Goner的定义",slug:"goner的定义"},{level:3,title:"在Gone中是如何完成依赖注入的?",slug:"在gone中是如何完成依赖注入的"},{level:3,title:"如何执行Goner中的业务代码?",slug:"如何执行goner中的业务代码"},{level:3,title:"gone命令,自动生成Priest函数",slug:"gone命令-自动生成priest函数"},{level:2,title:"匿名注入与具名注入",slug:"匿名注入与具名注入"},{level:3,title:"具名埋葬",slug:"具名埋葬"},{level:2,title:"指针注入、值注入 和 接口注入",slug:"指针注入、值注入-和-接口注入"},{level:3,title:"指针注入 vs 接口注入",slug:"指针注入-vs-接口注入"},{level:2,title:"Slice注入和 Map注入",slug:"slice注入和-map注入"},{level:2,title:"私有属性注入",slug:"私有属性注入"},{level:2,title:"配置注入",slug:"配置注入"}]},{title:"利用redis提供分布式锁和分布式缓存",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./schedule"},regularPath:"/zh/guide/redis.html",relativePath:"zh/guide/redis.md",key:"v-6a7c97c5",path:"/zh/guide/redis.html",headers:[{level:2,title:"第一步:将redis相关Goner埋葬到Cemetery",slug:"第一步-将redis相关goner埋葬到cemetery"},{level:2,title:"第二步:在配置文件中增加redis相关配置",slug:"第二步-在配置文件中增加redis相关配置"},{level:2,title:"第三步,使用redis",slug:"第三步-使用redis"},{level:3,title:"注入接口",slug:"注入接口"},{level:3,title:"使用分布是缓存",slug:"使用分布是缓存"},{level:3,title:"使用分布时锁",slug:"使用分布时锁"},{level:2,title:"上面例子完整代码",slug:"上面例子完整代码"}]},{title:"使用traceId追踪日志",frontmatter:{sidebar:"auto",prev:"./logrus",next:"./xorm"},regularPath:"/zh/guide/tracer.html",relativePath:"zh/guide/tracer.md",key:"v-3a551ac7",path:"/zh/guide/tracer.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"简单使用",slug:"简单使用"},{level:2,title:"跨协程(goroutine)传递traceId",slug:"跨协程-goroutine-传递traceid"},{level:2,title:"跨进程/服务传递traceId",slug:"跨进程-服务传递traceid"},{level:2,title:"多语言支持",slug:"多语言支持"}]},{title:"单元测试",frontmatter:{sidebar:"auto",prev:"./error",next:!1},regularPath:"/zh/guide/unit-test.html",relativePath:"zh/guide/unit-test.md",key:"v-71628cc5",path:"/zh/guide/unit-test.html",headers:[{level:2,title:"假设我们编写的Goner如下",slug:"假设我们编写的goner如下"},{level:2,title:"我们可以编写测试文件如下:",slug:"我们可以编写测试文件如下"},{level:2,title:"使用gomock做mock测试",slug:"使用gomock做mock测试"}]},{title:"通过内置Goners支持数据库操作",frontmatter:{sidebar:"auto",prev:"./tracer",next:"./redis"},regularPath:"/zh/guide/xorm.html",relativePath:"zh/guide/xorm.md",key:"v-36b8ee32",path:"/zh/guide/xorm.html",headers:[{level:2,title:"以Mysql举个例子,源代码在这里可以找到",slug:"以mysql举个例子-源代码在这里可以找到"},{level:3,title:"1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务",slug:"_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"},{level:3,title:"2. 创建mod和配置",slug:"_2-创建mod和配置"},{level:3,title:"3. 编写代码如下:",slug:"_3-编写代码如下"},{level:3,title:"3. 运行代码",slug:"_3-运行代码"},{level:2,title:"需要注意的点",slug:"需要注意的点"},{level:3,title:"1. github.com/gone-io/gone/goner 是对 xorm.io/xorm 的封装",slug:"_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"},{level:3,title:"2. 通过gone-xormGonerId注入",slug:"_2-通过gone-xormgonerid注入"},{level:3,title:"3. 注意导入的包",slug:"_3-注意导入的包"}]},{title:"Gone支持哪些方式注入?",frontmatter:{sidebar:"auto",prev:"./gone-and-inject",next:"./gone-vs-spring"},regularPath:"/zh/guide/goner-inject.html",relativePath:"zh/guide/goner-inject.md",key:"v-e8628a72",path:"/zh/guide/goner-inject.html",headers:[{level:2,title:"共有属性注入 vs 私有属性注入",slug:"共有属性注入-vs-私有属性注入"},{level:2,title:"匿名注入 vs 具名注入",slug:"匿名注入-vs-具名注入"},{level:3,title:"匿名埋葬 和 具名埋葬",slug:"匿名埋葬-和-具名埋葬"},{level:3,title:"匿名注入",slug:"匿名注入"},{level:3,title:"具名注入",slug:"具名注入"},{level:2,title:"支持注入的属性类型",slug:"支持注入的属性类型"},{level:3,title:"1.指针类型",slug:"_1-指针类型"},{level:3,title:"2.值类型",slug:"_2-值类型"},{level:3,title:"3.接口类型",slug:"_3-接口类型"},{level:3,title:"4.Slice 类型",slug:"_4-slice-类型"},{level:3,title:"5.Map 类型",slug:"_5-map-类型"},{level:3,title:"指针注入 vs 接口注入",slug:"指针注入-vs-接口注入"},{level:3,title:"Slice注入 vs Map注入",slug:"slice注入-vs-map注入"}]},{title:"封装一个Goner调用GPT",frontmatter:{sidebar:"auto",prev:"./mysql"},regularPath:"/zh/quick-start/gpt.html",relativePath:"zh/quick-start/gpt.md",key:"v-0f448959",path:"/zh/quick-start/gpt.html",headers:[{level:2,title:"定义用于依赖注入的接口",slug:"定义用于依赖注入的接口"},{level:2,title:"实现这个接口并嵌入gone.Flag",slug:"实现这个接口并嵌入gone-flag"},{level:2,title:"设计配置项",slug:"设计配置项"},{level:2,title:"使chatGPTClient实现接口AfterRevive() error函数,称为“先知”",slug:"使chatgptclient实现接口afterrevive-error函数-称为-先知"},{level:2,title:"方便用户使用",slug:"方便用户使用"},{level:2,title:"完整的疯转代码如下",slug:"完整的疯转代码如下"},{level:2,title:"编写测试",slug:"编写测试"},{level:2,title:"使用和编写示例",slug:"使用和编写示例"}]},{title:"快速开始",frontmatter:{sidebar:"auto",prev:"../",next:"../guide/"},regularPath:"/zh/quick-start/",relativePath:"zh/quick-start/README.md",key:"v-3f115d44",path:"/zh/quick-start/",headers:[{level:2,title:"创建一个简单的Web项目",slug:"创建一个简单的web项目"},{level:2,title:"创建一个使用MySql做数据库,可以应用到生产环境的项目",slug:"创建一个使用mysql做数据库-可以应用到生产环境的项目"},{level:2,title:"更多例子",slug:"更多例子"}]},{title:"JWT + MySQL + Redis + Schedule",frontmatter:{sidebar:"auto",prev:"./mysql",next:!1},regularPath:"/zh/quick-start/jwt+mysql+redis+schedule.html",relativePath:"zh/quick-start/jwt+mysql+redis+schedule.md",key:"v-bce8af32",path:"/zh/quick-start/jwt+mysql+redis+schedule.html",headers:[{level:2,title:"业务和接口说明",slug:"业务和接口说明"}]},{title:"Web 项目",frontmatter:{sidebar:"auto",prev:!1,next:"./mysql"},regularPath:"/zh/quick-start/web.html",relativePath:"zh/quick-start/web.md",key:"v-7e64f407",path:"/zh/quick-start/web.html",headers:[{level:2,title:"安装gone辅助工具",slug:"安装gone辅助工具"},{level:2,title:"创建一个web项目并运行代码",slug:"创建一个web项目并运行代码"},{level:2,title:"项目结构",slug:"项目结构"},{level:2,title:"Router",slug:"router"},{level:2,title:"Controller",slug:"controller"},{level:2,title:"Service",slug:"service"},{level:2,title:"数据库操作",slug:"数据库操作"}]},{title:"一个极简例子",frontmatter:{sidebar:"auto",prev:!1,next:"./web"},regularPath:"/zh/quick-start/simple.html",relativePath:"zh/quick-start/simple.md",key:"v-4964c3a7",path:"/zh/quick-start/simple.html"},{title:"参考文档",frontmatter:{sidebar:"auto",prev:"../guide/",next:"../goners/"},regularPath:"/zh/references/",relativePath:"zh/references/README.md",key:"v-7f6e5700",path:"/zh/references/",headers:[{level:2,title:"gone 辅助工具",slug:"gone-辅助工具"},{level:2,title:"HTTP 注入说明",slug:"http-注入说明"},{level:2,title:"Gone对Xorm的增强",slug:"gone对xorm的增强"},{level:2,title:"Redis 参考文档",slug:"redis-参考文档"}]},{title:"Web + MySQL",frontmatter:{sidebar:"auto",prev:"./web",next:"./gpt"},regularPath:"/zh/quick-start/mysql.html",relativePath:"zh/quick-start/mysql.md",key:"v-e844302a",path:"/zh/quick-start/mysql.html",headers:[{level:2,title:"安装gone辅助工具",slug:"安装gone辅助工具"},{level:2,title:"使用gone命令创建项目",slug:"使用gone命令创建项目"},{level:2,title:"编译并运行",slug:"编译并运行"},{level:2,title:"测试接口",slug:"测试接口"},{level:2,title:"项目简单介绍",slug:"项目简单介绍"},{level:3,title:"目录结构",slug:"目录结构"},{level:3,title:"项目特点",slug:"项目特点"},{level:3,title:"部分代码讲解",slug:"部分代码讲解"},{level:3,title:"HTTP 注入",slug:"http-注入"},{level:3,title:"请求处理函数参数",slug:"请求处理函数参数"}]},{title:"gone 辅助工具",frontmatter:{sidebar:"auto",prev:"./",next:"./http-inject"},regularPath:"/zh/references/gone-tool.html",relativePath:"zh/references/gone-tool.md",key:"v-347d4327",path:"/zh/references/gone-tool.html",headers:[{level:2,title:"安装",slug:"安装"},{level:2,title:"使用",slug:"使用"},{level:3,title:"测试是否正常安装",slug:"测试是否正常安装"},{level:3,title:"生成项目",slug:"生成项目"},{level:3,title:"生成Priest函数",slug:"生成priest函数"},{level:3,title:"生成Mock代码",slug:"生成mock代码"}]},{title:"用cron表达式配置定时任务",frontmatter:{sidebar:"auto",prev:"./redis",next:"./auto-gen-priest"},regularPath:"/zh/guide/schedule.html",relativePath:"zh/guide/schedule.md",key:"v-555fc7c7",path:"/zh/guide/schedule.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"编写定时任务执行的Job函数",slug:"编写定时任务执行的job函数"},{level:2,title:"设置定时任务",slug:"设置定时任务"},{level:2,title:"将定时配置放到配置文件中",slug:"将定时配置放到配置文件中"}]},{title:"Gone对Xorm的增强",frontmatter:{sidebar:"auto",prev:"./http-inject",next:"./redis"},regularPath:"/zh/references/xorm.html",relativePath:"zh/references/xorm.md",key:"v-40feb13a",path:"/zh/references/xorm.html",headers:[{level:2,title:"1. 自动事务",slug:"_1-自动事务"},{level:2,title:"2. 事务自动传递",slug:"_2-事务自动传递"},{level:2,title:"3. SQL支持名字参数",slug:"_3-sql支持名字参数"}]},{title:"Redis 参考文档",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./"},regularPath:"/zh/references/redis.html",relativePath:"zh/references/redis.md",key:"v-735b4bc7",path:"/zh/references/redis.html",headers:[{level:2,title:"配置项",slug:"配置项"},{level:2,title:"import 和 bury",slug:"import-和-bury"},{level:2,title:"使用分布是缓存 redis.Cache",slug:"使用分布是缓存-redis-cache"},{level:3,title:"接口定义",slug:"接口定义"},{level:3,title:"使用示例",slug:"使用示例"},{level:2,title:"使用分布式锁 redis.Locker",slug:"使用分布式锁-redis-locker"},{level:3,title:"接口定义",slug:"接口定义-2"},{level:3,title:"使用示例",slug:"使用示例-2"},{level:2,title:"操作Key,使用 redis.Key",slug:"操作key-使用-redis-key"},{level:3,title:"接口定义",slug:"接口定义-3"},{level:2,title:"使用 Provider 注入 redis 接口",slug:"使用-provider-注入-redis-接口"},{level:3,title:"使用示例",slug:"使用示例-3"},{level:2,title:"直接使用redis连接池",slug:"直接使用redis连接池"},{level:3,title:"接口定义",slug:"接口定义-4"},{level:3,title:"使用示例",slug:"使用示例-4"}]},{title:"HTTP 注入说明",frontmatter:{sidebar:"auto",prev:"./gone-tool",next:"./xorm"},regularPath:"/zh/references/http-inject.html",relativePath:"zh/references/http-inject.md",key:"v-3c133eb2",path:"/zh/references/http-inject.html",headers:[{level:2,title:"HTTP 依赖注入标签的格式",slug:"http-依赖注入标签的格式"},{level:2,title:"支持注入的类型和响应标签",slug:"支持注入的类型和响应标签"},{level:2,title:"Query参数注入",slug:"query参数注入"},{level:3,title:"属性类型为简单类型",slug:"属性类型为简单类型-1"},{level:3,title:"属性类型为简单类型的数组",slug:"属性类型为简单类型的数组"},{level:3,title:"属性类型为结构体或者结构体指针",slug:"属性类型为结构体或者结构体指针"},{level:2,title:"URL路径参数注入",slug:"url路径参数注入"},{level:2,title:"Body注入",slug:"body注入"},{level:2,title:"请求头注入",slug:"请求头注入"},{level:2,title:"Cookie注入",slug:"cookie注入"},{level:2,title:"高级",slug:"高级"},{level:3,title:"URL结构体注入",slug:"url结构体注入"},{level:3,title:"请求头注入",slug:"请求头注入-2"},{level:3,title:"上下文结构体注入",slug:"上下文结构体注入"},{level:3,title:"请求结构体注入",slug:"请求结构体注入"},{level:3,title:"请求响应接口注入",slug:"请求响应接口注入"},{level:2,title:"备注",slug:"备注"}]}],themeConfig:{repo:"gone-io/gone",docsDir:"docs",logo:"/assets/img/logo.png",locales:{"/":{label:"English",selectText:"Languages",editLinkText:"Edit this page on GitHub",nav:[{text:"Introduction",link:"/"},{text:"Quick Start",link:"/quick-start/",items:[{text:"Creating a Simple Web Project",link:"/quick-start/simple.md"},{text:"Web + MySQL",link:"/quick-start/mysql.md"}]},{text:"Development Guide",link:"/guide/",items:[{text:"Core Concepts of Gone",link:"/guide/core-concept.md"},{text:"Goner and Dependency Injection",link:"/guide/gone-and-inject.md"},{text:"Injection Methods Supported by Gone",link:"/guide/goner-inject.md"},{text:"How to Gracefully Use Built-in Goners?",link:"/guide/inner-goner.md"},{text:"Configuring with Built-in Goners Support",link:"/guide/config.md"},{text:"Performing database operations",link:"/guide/xorm.md"},{text:"Gone's Hook Functions",link:"/guide/hooks.md"},{text:"Logging Output",link:"/guide/logrus.md"},{text:"Trace Log with traceId",link:"/guide/tracer.md"},{text:"Auto-generate Priest",link:"/guide/auto-gen-priest.md"},{text:"Using Redis for Distributed Locking and Caching",link:"/guide/redis"},{text:"Unit Testing",link:"/guide/unit-test.md"}]},{text:"References",link:"/references/"},{text:"Goners",link:"/goners/"}],sidebar:[["/","介绍"],"/quick-start/","/guide/","/guide/core-concept"]},"/zh/":{label:"简体中文",selectText:"选择语言",editLinkText:"在 GitHub 上编辑此页",nav:[{text:"介绍",link:"/zh/"},{text:"快速开始",link:"/zh/quick-start/",items:[{text:"极简例子",link:"/zh/quick-start/simple.md"},{text:"Web项目",link:"/zh/quick-start/web.md"},{text:"Web + MySQL",link:"/zh/quick-start/mysql.md"},{text:"封装一个Goner调用GPT",link:"/zh/quick-start/gpt.md"}]},{text:"开发指南",link:"/zh/guide/",items:[{text:"核心概念",link:"/zh/guide/core-concept.md"},{text:"依赖注入",items:[{text:"Goner和依赖注入",link:"/zh/guide/gone-and-inject.md"},{text:"支持哪些方式注入?",link:"/zh/guide/goner-inject.md"},{text:"Gone vs Spring",link:"/zh/guide/gone-vs-spring.md"}]},{text:"内置模块使用",items:[{text:"内置Goners",link:"/zh/guide/inner-goner.md"},{text:"配置读取",link:"/zh/guide/config.md"},{text:"日志输出",link:"/zh/guide/logrus.md"},{text:"链路追踪",link:"/zh/guide/tracer.md"},{text:"操作数据库",link:"/zh/guide/xorm.md"},{text:"分布式锁 + 分布式缓存",link:"/zh/guide/redis"},{text:"cron 格式的任务",link:"/zh/guide/schedule.md"}]},{text:"效率提升",items:[{text:"自动生成 Priest",link:"/zh/guide/auto-gen-priest.md"},{text:"Hook 函数",link:"/zh/guide/hooks.md"},{text:"错误处理",link:"/zh/guide/error.md"},{text:"单元测试",link:"/zh/guide/unit-test.md"}]}]},{text:"References",link:"/zh/references/"},{text:"Goners",link:"/zh/goners/"},{text:"博客",link:"/zh/blogs/"}],sidebar:["/zh/","/zh/quick-start/","/zh/guide/","/zh/guide/core-concept"]}}},locales:{"/zh/":{lang:"zh-CN",title:"Gone 文档",description:"Gone是一个轻量的,基于Golang的,依赖注入框架,集成了Gin、XOrm、Logrus等众多开源工具,用于提供便捷的Web开发。",path:"/zh/"},"/":{lang:"en-US",title:"Gone Document",description:"Gone is a lightweight dependency injection framework based on Golang, integrating many open-source tools such as Gin, XOrm, Logrus, etc., to provide convenient web development.",path:"/"}}};n(236);Wn.component("Badge",()=>Promise.all([n.e(0),n.e(4)]).then(n.bind(null,312))),Wn.component("CodeBlock",()=>Promise.all([n.e(0),n.e(5)]).then(n.bind(null,308))),Wn.component("CodeGroup",()=>Promise.all([n.e(0),n.e(6)]).then(n.bind(null,309)));n(237);var As={props:{color:{required:!1,default:"rgb(66, 185, 131)"}}},zs=(n(238),Object(Os.a)(As,(function(){return(0,this._self._c)("div",{staticClass:"spinner",style:{background:this.color}})}),[],!1,null,"1bbcb91a",null).exports);const Ls={name:"Mermaid",props:{id:{type:String,required:!1,default:()=>"diagram_"+Date.now()},graph:{type:String,required:!1}},data:()=>({svg:void 0}),computed:{graphData(){return this.graph?this.graph:this.$slots.default[0].text}},render(e){return void 0===this.svg?e("Loading"):e("div",{class:["mermaid-diagram"],domProps:{innerHTML:this.svg,style:"width: 100%"}})},mounted(){n.e(74).then(n.t.bind(null,304,7)).then(e=>{e.initialize({startOnLoad:!0}),e.render(this.id,this.graphData,e=>{this.svg=e})})},components:{Loading:zs}};var Is=n(45);const Rs={locales:!0};var Ms=[{},({Vue:e})=>{e.mixin({computed:{$dataBlock(){return this.$options.__data__block__}}})},{},{},({Vue:e})=>{e.component(Ls.name,Ls)},({router:e,siteData:t})=>{const{routes:n=[]}=e.options,{redirectors:r=[]}=Rs;function o(e){return n.some(t=>t.path.toLowerCase()===e.toLowerCase())}function i(e){if(o(e))return e;if(!/\/$/.test(e)){const t=e+"/";if(o(t))return t}if(!/\.html$/.test(e)){const t=e.replace(/\/$/,"")+".html";if(o(t))return t}return null}if(Rs.locales&&t.locales){const e=t.locales,n=Object.keys(e),o=n.map(t=>({key:t.replace(/^\/|\/$/,""),lang:e[t].lang}));"object"!=typeof Rs.locales&&(Rs.locales={});const{fallback:i,storage:a=!0}=Rs.locales;i&&n.unshift(i),r.unshift({storage:a,base:"/",alternative(){if("undefined"!=typeof window&&window.navigator){const e=window.navigator.languages||[window.navigator.language],t=o.find(({lang:t})=>e.includes(t));if(t)return t.key}return n}})}const a=r.map(({base:e="/",storage:t=!1,alternative:n})=>{let r=!1;if(t)if("object"!=typeof t){const n="string"!=typeof t?"vuepress:redirect:"+e:t;r={get:()=>"undefined"==typeof localStorage?null:localStorage.getItem(n),set(e){"undefined"!=typeof localStorage&&localStorage.setItem(n,e)}}}else t.get&&t.set&&(r=t);return{base:e,storage:r,alternative:n}});e.beforeEach((e,t,n)=>{if(i(e.path))return n();let r;for(const t of a){const{base:n="/",storage:o=!1}=t;let{alternative:a}=t;if(!e.path.startsWith(n))continue;const s=e.path.slice(n.length)||"/";if(o){const e=o.get(t);if(e){const t=i(Object(Is.join)(n,e,s));if(t){r=t;break}}}if("function"==typeof a&&(a=a(s)),a){"string"==typeof a&&(a=[a]);for(const e of a){const t=i(Object(Is.join)(n,e,s));if(t){r=t;break}}if(r)break}}n(r)}),e.afterEach(e=>{if(o(e.path))for(const t of a){const{base:n,storage:r}=t;if(!r||!e.path.startsWith(n))continue;const o=e.path.slice(n.length).split("/")[0];o&&r.set(o,t)}})}],Ds=[];class qs extends class{constructor(){this.store=new Wn({data:{state:{}}})}$get(e){return this.store.state[e]}$set(e,t){Wn.set(this.store.state,e,t)}$emit(...e){this.store.$emit(...e)}$on(...e){this.store.$on(...e)}}{}Object.assign(qs.prototype,{getPageAsyncComponent:ss,getLayoutAsyncComponent:ls,getAsyncComponent:us,getVueComponent:cs});var Gs={install(e){const t=new qs;e.$vuepress=t,e.prototype.$vuepress=t}};function Ns(e,t){const n=t.toLowerCase();return e.options.routes.some(e=>e.path.toLowerCase()===n)}var Fs={props:{pageKey:String,slotKey:{type:String,default:"default"}},render(e){const t=this.pageKey||this.$parent.$page.key;return ps("pageKey",t),Wn.component(t)||Wn.component(t,ss(t)),Wn.component(t)?e(t):e("")}},Us={functional:!0,props:{slotKey:String,required:!0},render:(e,{props:t,slots:n})=>e("div",{class:["content__"+t.slotKey]},n()[t.slotKey])},Bs={computed:{openInNewWindowTitle(){return this.$themeLocaleConfig.openNewWindowText||"(opens new window)"}}},Hs=(n(239),n(240),Object(Os.a)(Bs,(function(){var e=this._self._c;return e("span",[e("svg",{staticClass:"icon outbound",attrs:{xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",focusable:"false",x:"0px",y:"0px",viewBox:"0 0 100 100",width:"15",height:"15"}},[e("path",{attrs:{fill:"currentColor",d:"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"}}),this._v(" "),e("polygon",{attrs:{fill:"currentColor",points:"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"}})]),this._v(" "),e("span",{staticClass:"sr-only"},[this._v(this._s(this.openInNewWindowTitle))])])}),[],!1,null,null,null).exports),Vs={functional:!0,render(e,{parent:t,children:n}){if(t._isMounted)return n;t.$once("hook:mounted",()=>{t.$forceUpdate()})}};Wn.config.productionTip=!1,Wn.use(Ha),Wn.use(Gs),Wn.mixin(function(e,t,n=Wn){!function(e){e.locales&&Object.keys(e.locales).forEach(t=>{e.locales[t].path=t});Object.freeze(e)}(t),n.$vuepress.$set("siteData",t);const r=new(e(n.$vuepress.$get("siteData"))),o=Object.getOwnPropertyDescriptors(Object.getPrototypeOf(r)),i={};return Object.keys(o).reduce((e,t)=>(t.startsWith("$")&&(e[t]=o[t].get),e),i),{computed:i}}(e=>class{setPage(e){this.__page=e}get $site(){return e}get $themeConfig(){return this.$site.themeConfig}get $frontmatter(){return this.$page.frontmatter}get $localeConfig(){const{locales:e={}}=this.$site;let t,n;for(const r in e)"/"===r?n=e[r]:0===this.$page.path.indexOf(r)&&(t=e[r]);return t||n||{}}get $siteTitle(){return this.$localeConfig.title||this.$site.title||""}get $canonicalUrl(){const{canonicalUrl:e}=this.$page.frontmatter;return"string"==typeof e&&e}get $title(){const e=this.$page,{metaTitle:t}=this.$page.frontmatter;if("string"==typeof t)return t;const n=this.$siteTitle,r=e.frontmatter.home?null:e.frontmatter.title||e.title;return n?r?r+" | "+n:n:r||"VuePress"}get $description(){const e=function(e){if(e){const t=e.filter(e=>"description"===e.name)[0];if(t)return t.content}}(this.$page.frontmatter.meta);return e||(this.$page.frontmatter.description||this.$localeConfig.description||this.$site.description||"")}get $lang(){return this.$page.frontmatter.lang||this.$localeConfig.lang||"en-US"}get $localePath(){return this.$localeConfig.path||"/"}get $themeLocaleConfig(){return(this.$site.themeConfig.locales||{})[this.$localePath]||{}}get $page(){return this.__page?this.__page:function(e,t){for(let n=0;nn||(e.hash?!Wn.$vuepress.$get("disableScrollBehavior")&&{selector:decodeURIComponent(e.hash)}:{x:0,y:0})});!function(e){e.beforeEach((t,n,r)=>{if(Ns(e,t.path))r();else if(/(\/|\.html)$/.test(t.path))if(/\/$/.test(t.path)){const n=t.path.replace(/\/$/,"")+".html";Ns(e,n)?r(n):r()}else r();else{const n=t.path+"/",o=t.path+".html";Ns(e,o)?r(o):Ns(e,n)?r(n):r()}})}(n);const r={};try{await Promise.all(Ms.filter(e=>"function"==typeof e).map(t=>t({Vue:Wn,options:r,router:n,siteData:Ts,isServer:e})))}catch(e){console.error(e)}return{app:new Wn(Object.assign(r,{router:n,render:e=>e("div",{attrs:{id:"app"}},[e("RouterView",{ref:"layout"}),e("div",{class:"global-ui"},Ds.map(t=>e(t)))])})),router:n}}(!1).then(({app:e,router:t})=>{t.onReady(()=>{e.$mount("#app")})})}]); \ No newline at end of file +var r=Object.freeze({}),o=Array.isArray;function i(e){return null==e}function a(e){return null!=e}function s(e){return!0===e}function l(e){return"string"==typeof e||"number"==typeof e||"symbol"==typeof e||"boolean"==typeof e}function u(e){return"function"==typeof e}function c(e){return null!==e&&"object"==typeof e}var f=Object.prototype.toString;function p(e){return"[object Object]"===f.call(e)}function h(e){return"[object RegExp]"===f.call(e)}function d(e){var t=parseFloat(String(e));return t>=0&&Math.floor(t)===t&&isFinite(e)}function v(e){return a(e)&&"function"==typeof e.then&&"function"==typeof e.catch}function g(e){return null==e?"":Array.isArray(e)||p(e)&&e.toString===f?JSON.stringify(e,m,2):String(e)}function m(e,t){return t&&t.__v_isRef?t.value:t}function y(e){var t=parseFloat(e);return isNaN(t)?e:t}function b(e,t){for(var n=Object.create(null),r=e.split(","),o=0;o-1)return e.splice(r,1)}}var w=Object.prototype.hasOwnProperty;function k(e,t){return w.call(e,t)}function j(e){var t=Object.create(null);return function(n){return t[n]||(t[n]=e(n))}}var C=/-(\w)/g,P=j((function(e){return e.replace(C,(function(e,t){return t?t.toUpperCase():""}))})),S=j((function(e){return e.charAt(0).toUpperCase()+e.slice(1)})),O=/\B([A-Z])/g,$=j((function(e){return e.replace(O,"-$1").toLowerCase()}));var E=Function.prototype.bind?function(e,t){return e.bind(t)}:function(e,t){function n(n){var r=arguments.length;return r?r>1?e.apply(t,arguments):e.call(t,n):e.call(t)}return n._length=e.length,n};function T(e,t){t=t||0;for(var n=e.length-t,r=new Array(n);n--;)r[n]=e[n+t];return r}function A(e,t){for(var n in t)e[n]=t[n];return e}function z(e){for(var t={},n=0;n0,Z=X&&X.indexOf("edge/")>0;X&&X.indexOf("android");var ee=X&&/iphone|ipad|ipod|ios/.test(X);X&&/chrome\/\d+/.test(X),X&&/phantomjs/.test(X);var te,ne=X&&X.match(/firefox\/(\d+)/),re={}.watch,oe=!1;if(Q)try{var ie={};Object.defineProperty(ie,"passive",{get:function(){oe=!0}}),window.addEventListener("test-passive",null,ie)}catch(e){}var ae=function(){return void 0===te&&(te=!Q&&"undefined"!=typeof global&&(global.process&&"server"===global.process.env.VUE_ENV)),te},se=Q&&window.__VUE_DEVTOOLS_GLOBAL_HOOK__;function le(e){return"function"==typeof e&&/native code/.test(e.toString())}var ue,ce="undefined"!=typeof Symbol&&le(Symbol)&&"undefined"!=typeof Reflect&&le(Reflect.ownKeys);ue="undefined"!=typeof Set&&le(Set)?Set:function(){function e(){this.set=Object.create(null)}return e.prototype.has=function(e){return!0===this.set[e]},e.prototype.add=function(e){this.set[e]=!0},e.prototype.clear=function(){this.set=Object.create(null)},e}();var fe=null;function pe(e){void 0===e&&(e=null),e||fe&&fe._scope.off(),fe=e,e&&e._scope.on()}var he=function(){function e(e,t,n,r,o,i,a,s){this.tag=e,this.data=t,this.children=n,this.text=r,this.elm=o,this.ns=void 0,this.context=i,this.fnContext=void 0,this.fnOptions=void 0,this.fnScopeId=void 0,this.key=t&&t.key,this.componentOptions=a,this.componentInstance=void 0,this.parent=void 0,this.raw=!1,this.isStatic=!1,this.isRootInsert=!0,this.isComment=!1,this.isCloned=!1,this.isOnce=!1,this.asyncFactory=s,this.asyncMeta=void 0,this.isAsyncPlaceholder=!1}return Object.defineProperty(e.prototype,"child",{get:function(){return this.componentInstance},enumerable:!1,configurable:!0}),e}(),de=function(e){void 0===e&&(e="");var t=new he;return t.text=e,t.isComment=!0,t};function ve(e){return new he(void 0,void 0,void 0,String(e))}function ge(e){var t=new he(e.tag,e.data,e.children&&e.children.slice(),e.text,e.elm,e.context,e.componentOptions,e.asyncFactory);return t.ns=e.ns,t.isStatic=e.isStatic,t.key=e.key,t.isComment=e.isComment,t.fnContext=e.fnContext,t.fnOptions=e.fnOptions,t.fnScopeId=e.fnScopeId,t.asyncMeta=e.asyncMeta,t.isCloned=!0,t}"function"==typeof SuppressedError&&SuppressedError;var me=0,ye=[],be=function(){function e(){this._pending=!1,this.id=me++,this.subs=[]}return e.prototype.addSub=function(e){this.subs.push(e)},e.prototype.removeSub=function(e){this.subs[this.subs.indexOf(e)]=null,this._pending||(this._pending=!0,ye.push(this))},e.prototype.depend=function(t){e.target&&e.target.addDep(this)},e.prototype.notify=function(e){var t=this.subs.filter((function(e){return e}));for(var n=0,r=t.length;n0&&(Xe((u=e(u,"".concat(n||"","_").concat(r)))[0])&&Xe(f)&&(p[c]=ve(f.text+u[0].text),u.shift()),p.push.apply(p,u)):l(u)?Xe(f)?p[c]=ve(f.text+u):""!==u&&p.push(ve(u)):Xe(u)&&Xe(f)?p[c]=ve(f.text+u.text):(s(t._isVList)&&a(u.tag)&&i(u.key)&&a(n)&&(u.key="__vlist".concat(n,"_").concat(r,"__")),p.push(u)));return p}(e):void 0}function Xe(e){return a(e)&&a(e.text)&&!1===e.isComment}function Je(e,t){var n,r,i,s,l=null;if(o(e)||"string"==typeof e)for(l=new Array(e.length),n=0,r=e.length;n0,s=t?!!t.$stable:!a,l=t&&t.$key;if(t){if(t._normalized)return t._normalized;if(s&&o&&o!==r&&l===o.$key&&!a&&!o.$hasNormal)return o;for(var u in i={},t)t[u]&&"$"!==u[0]&&(i[u]=gt(e,n,u,t[u]))}else i={};for(var c in n)c in i||(i[c]=mt(n,c));return t&&Object.isExtensible(t)&&(t._normalized=i),V(i,"$stable",s),V(i,"$key",l),V(i,"$hasNormal",a),i}function gt(e,t,n,r){var i=function(){var t=fe;pe(e);var n=arguments.length?r.apply(null,arguments):r({}),i=(n=n&&"object"==typeof n&&!o(n)?[n]:Qe(n))&&n[0];return pe(t),n&&(!i||1===n.length&&i.isComment&&!dt(i))?void 0:n};return r.proxy&&Object.defineProperty(t,n,{get:i,enumerable:!0,configurable:!0}),i}function mt(e,t){return function(){return e[t]}}function yt(e){return{get attrs(){if(!e._attrsProxy){var t=e._attrsProxy={};V(t,"_v_attr_proxy",!0),bt(t,e.$attrs,r,e,"$attrs")}return e._attrsProxy},get listeners(){e._listenersProxy||bt(e._listenersProxy={},e.$listeners,r,e,"$listeners");return e._listenersProxy},get slots(){return function(e){e._slotsProxy||xt(e._slotsProxy={},e.$scopedSlots);return e._slotsProxy}(e)},emit:E(e.$emit,e),expose:function(t){t&&Object.keys(t).forEach((function(n){return Ge(e,t,n)}))}}}function bt(e,t,n,r,o){var i=!1;for(var a in t)a in e?t[a]!==n[a]&&(i=!0):(i=!0,_t(e,a,r,o));for(var a in e)a in t||(i=!0,delete e[a]);return i}function _t(e,t,n,r){Object.defineProperty(e,t,{enumerable:!0,configurable:!0,get:function(){return n[r][t]}})}function xt(e,t){for(var n in t)e[n]=t[n];for(var n in e)n in t||delete e[n]}var wt=null;function kt(e,t){return(e.__esModule||ce&&"Module"===e[Symbol.toStringTag])&&(e=e.default),c(e)?t.extend(e):e}function jt(e){if(o(e))for(var t=0;tdocument.createEvent("Event").timeStamp&&(un=function(){return cn.now()})}var fn=function(e,t){if(e.post){if(!t.post)return 1}else if(t.post)return-1;return e.id-t.id};function pn(){var e,t;for(ln=un(),an=!0,tn.sort(fn),sn=0;snsn&&tn[n].id>e.id;)n--;tn.splice(n+1,0,e)}else tn.push(e);on||(on=!0,qt(pn))}}function dn(e,t){if(e){for(var n=Object.create(null),r=ce?Reflect.ownKeys(e):Object.keys(e),o=0;o-1)if(i&&!k(o,"default"))a=!1;else if(""===a||a===$(e)){var l=Rn(String,o.type);(l<0||s-1:"string"==typeof e?e.split(",").indexOf(t)>-1:!!h(e)&&e.test(t)}function Jn(e,t){var n=e.cache,r=e.keys,o=e._vnode,i=e.$vnode;for(var a in n){var s=n[a];if(s){var l=s.name;l&&!t(l)&&Yn(n,a,r,o)}}i.componentOptions.children=void 0}function Yn(e,t,n,r){var o=e[t];!o||r&&o.tag===r.tag||o.componentInstance.$destroy(),e[t]=null,x(n,t)}Wn.prototype._init=function(e){var t=this;t._uid=Hn++,t._isVue=!0,t.__v_skip=!0,t._scope=new Fe(!0),t._scope.parent=void 0,t._scope._vm=!0,e&&e._isComponent?function(e,t){var n=e.$options=Object.create(e.constructor.options),r=t._parentVnode;n.parent=t.parent,n._parentVnode=r;var o=r.componentOptions;n.propsData=o.propsData,n._parentListeners=o.listeners,n._renderChildren=o.children,n._componentTag=o.tag,t.render&&(n.render=t.render,n.staticRenderFns=t.staticRenderFns)}(t,e):t.$options=En(Vn(t.constructor),e||{},t),t._renderProxy=t,t._self=t,function(e){var t=e.$options,n=t.parent;if(n&&!t.abstract){for(;n.$options.abstract&&n.$parent;)n=n.$parent;n.$children.push(e)}e.$parent=n,e.$root=n?n.$root:e,e.$children=[],e.$refs={},e._provided=n?n._provided:Object.create(null),e._watcher=null,e._inactive=null,e._directInactive=!1,e._isMounted=!1,e._isDestroyed=!1,e._isBeingDestroyed=!1}(t),function(e){e._events=Object.create(null),e._hasHookEvent=!1;var t=e.$options._parentListeners;t&&Qt(e,t)}(t),function(e){e._vnode=null,e._staticTrees=null;var t=e.$options,n=e.$vnode=t._parentVnode,o=n&&n.context;e.$slots=pt(t._renderChildren,o),e.$scopedSlots=n?vt(e.$parent,n.data.scopedSlots,e.$slots):r,e._c=function(t,n,r,o){return Ct(e,t,n,r,o,!1)},e.$createElement=function(t,n,r,o){return Ct(e,t,n,r,o,!0)};var i=n&&n.data;Ae(e,"$attrs",i&&i.attrs||r,null,!0),Ae(e,"$listeners",t._parentListeners||r,null,!0)}(t),en(t,"beforeCreate",void 0,!1),function(e){var t=dn(e.$options.inject,e);t&&(Oe(!1),Object.keys(t).forEach((function(n){Ae(e,n,t[n])})),Oe(!0))}(t),qn(t),function(e){var t=e.$options.provide;if(t){var n=u(t)?t.call(e):t;if(!c(n))return;for(var r=Ue(e),o=ce?Reflect.ownKeys(n):Object.keys(n),i=0;i1?T(n):n;for(var r=T(arguments,1),o='event handler for "'.concat(e,'"'),i=0,a=n.length;iparseInt(this.max)&&Yn(e,t[0],t,this._vnode),this.vnodeToCache=null}}},created:function(){this.cache=Object.create(null),this.keys=[]},destroyed:function(){for(var e in this.cache)Yn(this.cache,e,this.keys)},mounted:function(){var e=this;this.cacheVNode(),this.$watch("include",(function(t){Jn(e,(function(e){return Xn(t,e)}))})),this.$watch("exclude",(function(t){Jn(e,(function(e){return!Xn(t,e)}))}))},updated:function(){this.cacheVNode()},render:function(){var e=this.$slots.default,t=jt(e),n=t&&t.componentOptions;if(n){var r=Qn(n),o=this.include,i=this.exclude;if(o&&(!r||!Xn(o,r))||i&&r&&Xn(i,r))return t;var a=this.cache,s=this.keys,l=null==t.key?n.Ctor.cid+(n.tag?"::".concat(n.tag):""):t.key;a[l]?(t.componentInstance=a[l].componentInstance,x(s,l),s.push(l)):(this.vnodeToCache=t,this.keyToCache=l),t.data.keepAlive=!0}return t||e&&e[0]}}};!function(e){var t={get:function(){return U}};Object.defineProperty(e,"config",t),e.util={warn:kn,extend:A,mergeOptions:En,defineReactive:Ae},e.set=ze,e.delete=Le,e.nextTick=qt,e.observable=function(e){return Te(e),e},e.options=Object.create(null),N.forEach((function(t){e.options[t+"s"]=Object.create(null)})),e.options._base=e,A(e.options.components,er),function(e){e.use=function(e){var t=this._installedPlugins||(this._installedPlugins=[]);if(t.indexOf(e)>-1)return this;var n=T(arguments,1);return n.unshift(this),u(e.install)?e.install.apply(e,n):u(e)&&e.apply(null,n),t.push(e),this}}(e),function(e){e.mixin=function(e){return this.options=En(this.options,e),this}}(e),Kn(e),function(e){N.forEach((function(t){e[t]=function(e,n){return n?("component"===t&&p(n)&&(n.name=n.name||e,n=this.options._base.extend(n)),"directive"===t&&u(n)&&(n={bind:n,update:n}),this.options[t+"s"][e]=n,n):this.options[t+"s"][e]}}))}(e)}(Wn),Object.defineProperty(Wn.prototype,"$isServer",{get:ae}),Object.defineProperty(Wn.prototype,"$ssrContext",{get:function(){return this.$vnode&&this.$vnode.ssrContext}}),Object.defineProperty(Wn,"FunctionalRenderContext",{value:vn}),Wn.version="2.7.16";var tr=b("style,class"),nr=b("input,textarea,option,select,progress"),rr=b("contenteditable,draggable,spellcheck"),or=b("events,caret,typing,plaintext-only"),ir=b("allowfullscreen,async,autofocus,autoplay,checked,compact,controls,declare,default,defaultchecked,defaultmuted,defaultselected,defer,disabled,enabled,formnovalidate,hidden,indeterminate,inert,ismap,itemscope,loop,multiple,muted,nohref,noresize,noshade,novalidate,nowrap,open,pauseonexit,readonly,required,reversed,scoped,seamless,selected,sortable,truespeed,typemustmatch,visible"),ar="http://www.w3.org/1999/xlink",sr=function(e){return":"===e.charAt(5)&&"xlink"===e.slice(0,5)},lr=function(e){return sr(e)?e.slice(6,e.length):""},ur=function(e){return null==e||!1===e};function cr(e){for(var t=e.data,n=e,r=e;a(r.componentInstance);)(r=r.componentInstance._vnode)&&r.data&&(t=fr(r.data,t));for(;a(n=n.parent);)n&&n.data&&(t=fr(t,n.data));return function(e,t){if(a(e)||a(t))return pr(e,hr(t));return""}(t.staticClass,t.class)}function fr(e,t){return{staticClass:pr(e.staticClass,t.staticClass),class:a(e.class)?[e.class,t.class]:t.class}}function pr(e,t){return e?t?e+" "+t:e:t||""}function hr(e){return Array.isArray(e)?function(e){for(var t,n="",r=0,o=e.length;r-1?Mr(e,t,n):ir(t)?ur(n)?e.removeAttribute(t):(n="allowfullscreen"===t&&"EMBED"===e.tagName?"true":t,e.setAttribute(t,n)):rr(t)?e.setAttribute(t,function(e,t){return ur(t)||"false"===t?"false":"contenteditable"===e&&or(t)?t:"true"}(t,n)):sr(t)?ur(n)?e.removeAttributeNS(ar,lr(t)):e.setAttributeNS(ar,t,n):Mr(e,t,n)}function Mr(e,t,n){if(ur(n))e.removeAttribute(t);else{if(J&&!Y&&"TEXTAREA"===e.tagName&&"placeholder"===t&&""!==n&&!e.__ieph){var r=function(t){t.stopImmediatePropagation(),e.removeEventListener("input",r)};e.addEventListener("input",r),e.__ieph=!0}e.setAttribute(t,n)}}var Dr={create:Ir,update:Ir};function qr(e,t){var n=t.elm,r=t.data,o=e.data;if(!(i(r.staticClass)&&i(r.class)&&(i(o)||i(o.staticClass)&&i(o.class)))){var s=cr(t),l=n._transitionClasses;a(l)&&(s=pr(s,hr(l))),s!==n._prevClass&&(n.setAttribute("class",s),n._prevClass=s)}}var Gr,Nr={create:qr,update:qr};function Fr(e,t,n){var r=Gr;return function o(){var i=t.apply(null,arguments);null!==i&&Hr(e,o,n,r)}}var Ur=Tt&&!(ne&&Number(ne[1])<=53);function Br(e,t,n,r){if(Ur){var o=ln,i=t;t=i._wrapper=function(e){if(e.target===e.currentTarget||e.timeStamp>=o||e.timeStamp<=0||e.target.ownerDocument!==document)return i.apply(this,arguments)}}Gr.addEventListener(e,t,oe?{capture:n,passive:r}:n)}function Hr(e,t,n,r){(r||Gr).removeEventListener(e,t._wrapper||t,n)}function Vr(e,t){if(!i(e.data.on)||!i(t.data.on)){var n=t.data.on||{},r=e.data.on||{};Gr=t.elm||e.elm,function(e){if(a(e.__r)){var t=J?"change":"input";e[t]=[].concat(e.__r,e[t]||[]),delete e.__r}a(e.__c)&&(e.change=[].concat(e.__c,e.change||[]),delete e.__c)}(n),Ve(n,r,Br,Hr,Fr,t.context),Gr=void 0}}var Wr,Kr={create:Vr,update:Vr,destroy:function(e){return Vr(e,jr)}};function Qr(e,t){if(!i(e.data.domProps)||!i(t.data.domProps)){var n,r,o=t.elm,l=e.data.domProps||{},u=t.data.domProps||{};for(n in(a(u.__ob__)||s(u._v_attr_proxy))&&(u=t.data.domProps=A({},u)),l)n in u||(o[n]="");for(n in u){if(r=u[n],"textContent"===n||"innerHTML"===n){if(t.children&&(t.children.length=0),r===l[n])continue;1===o.childNodes.length&&o.removeChild(o.childNodes[0])}if("value"===n&&"PROGRESS"!==o.tagName){o._value=r;var c=i(r)?"":String(r);Xr(o,c)&&(o.value=c)}else if("innerHTML"===n&&gr(o.tagName)&&i(o.innerHTML)){(Wr=Wr||document.createElement("div")).innerHTML="".concat(r,"");for(var f=Wr.firstChild;o.firstChild;)o.removeChild(o.firstChild);for(;f.firstChild;)o.appendChild(f.firstChild)}else if(r!==l[n])try{o[n]=r}catch(e){}}}}function Xr(e,t){return!e.composing&&("OPTION"===e.tagName||function(e,t){var n=!0;try{n=document.activeElement!==e}catch(e){}return n&&e.value!==t}(e,t)||function(e,t){var n=e.value,r=e._vModifiers;if(a(r)){if(r.number)return y(n)!==y(t);if(r.trim)return n.trim()!==t.trim()}return n!==t}(e,t))}var Jr={create:Qr,update:Qr},Yr=j((function(e){var t={},n=/:(.+)/;return e.split(/;(?![^(]*\))/g).forEach((function(e){if(e){var r=e.split(n);r.length>1&&(t[r[0].trim()]=r[1].trim())}})),t}));function Zr(e){var t=eo(e.style);return e.staticStyle?A(e.staticStyle,t):t}function eo(e){return Array.isArray(e)?z(e):"string"==typeof e?Yr(e):e}var to,no=/^--/,ro=/\s*!important$/,oo=function(e,t,n){if(no.test(t))e.style.setProperty(t,n);else if(ro.test(n))e.style.setProperty($(t),n.replace(ro,""),"important");else{var r=ao(t);if(Array.isArray(n))for(var o=0,i=n.length;o-1?t.split(uo).forEach((function(t){return e.classList.add(t)})):e.classList.add(t);else{var n=" ".concat(e.getAttribute("class")||""," ");n.indexOf(" "+t+" ")<0&&e.setAttribute("class",(n+t).trim())}}function fo(e,t){if(t&&(t=t.trim()))if(e.classList)t.indexOf(" ")>-1?t.split(uo).forEach((function(t){return e.classList.remove(t)})):e.classList.remove(t),e.classList.length||e.removeAttribute("class");else{for(var n=" ".concat(e.getAttribute("class")||""," "),r=" "+t+" ";n.indexOf(r)>=0;)n=n.replace(r," ");(n=n.trim())?e.setAttribute("class",n):e.removeAttribute("class")}}function po(e){if(e){if("object"==typeof e){var t={};return!1!==e.css&&A(t,ho(e.name||"v")),A(t,e),t}return"string"==typeof e?ho(e):void 0}}var ho=j((function(e){return{enterClass:"".concat(e,"-enter"),enterToClass:"".concat(e,"-enter-to"),enterActiveClass:"".concat(e,"-enter-active"),leaveClass:"".concat(e,"-leave"),leaveToClass:"".concat(e,"-leave-to"),leaveActiveClass:"".concat(e,"-leave-active")}})),vo=Q&&!Y,go="transition",mo="transitionend",yo="animation",bo="animationend";vo&&(void 0===window.ontransitionend&&void 0!==window.onwebkittransitionend&&(go="WebkitTransition",mo="webkitTransitionEnd"),void 0===window.onanimationend&&void 0!==window.onwebkitanimationend&&(yo="WebkitAnimation",bo="webkitAnimationEnd"));var _o=Q?window.requestAnimationFrame?window.requestAnimationFrame.bind(window):setTimeout:function(e){return e()};function xo(e){_o((function(){_o(e)}))}function wo(e,t){var n=e._transitionClasses||(e._transitionClasses=[]);n.indexOf(t)<0&&(n.push(t),co(e,t))}function ko(e,t){e._transitionClasses&&x(e._transitionClasses,t),fo(e,t)}function jo(e,t,n){var r=Po(e,t),o=r.type,i=r.timeout,a=r.propCount;if(!o)return n();var s="transition"===o?mo:bo,l=0,u=function(){e.removeEventListener(s,c),n()},c=function(t){t.target===e&&++l>=a&&u()};setTimeout((function(){l0&&(n="transition",c=a,f=i.length):"animation"===t?u>0&&(n="animation",c=u,f=l.length):f=(n=(c=Math.max(a,u))>0?a>u?"transition":"animation":null)?"transition"===n?i.length:l.length:0,{type:n,timeout:c,propCount:f,hasTransform:"transition"===n&&Co.test(r[go+"Property"])}}function So(e,t){for(;e.length1}function zo(e,t){!0!==t.data.show&&$o(t)}var Lo=function(e){var t,n,r={},u=e.modules,c=e.nodeOps;for(t=0;td?_(e,i(n[m+1])?null:n[m+1].elm,n,h,m,r):h>m&&w(t,f,d)}(f,v,m,n,u):a(m)?(a(e.text)&&c.setTextContent(f,""),_(f,null,m,0,m.length-1,n)):a(v)?w(v,0,v.length-1):a(e.text)&&c.setTextContent(f,""):e.text!==t.text&&c.setTextContent(f,t.text),a(d)&&a(h=d.hook)&&a(h=h.postpatch)&&h(e,t)}}}function P(e,t,n){if(s(n)&&a(e.parent))e.parent.data.pendingInsert=t;else for(var r=0;r-1,a.selected!==i&&(a.selected=i);else if(M(qo(a),r))return void(e.selectedIndex!==s&&(e.selectedIndex=s));o||(e.selectedIndex=-1)}}function Do(e,t){return t.every((function(t){return!M(t,e)}))}function qo(e){return"_value"in e?e._value:e.value}function Go(e){e.target.composing=!0}function No(e){e.target.composing&&(e.target.composing=!1,Fo(e.target,"input"))}function Fo(e,t){var n=document.createEvent("HTMLEvents");n.initEvent(t,!0,!0),e.dispatchEvent(n)}function Uo(e){return!e.componentInstance||e.data&&e.data.transition?e:Uo(e.componentInstance._vnode)}var Bo={model:Io,show:{bind:function(e,t,n){var r=t.value,o=(n=Uo(n)).data&&n.data.transition,i=e.__vOriginalDisplay="none"===e.style.display?"":e.style.display;r&&o?(n.data.show=!0,$o(n,(function(){e.style.display=i}))):e.style.display=r?i:"none"},update:function(e,t,n){var r=t.value;!r!=!t.oldValue&&((n=Uo(n)).data&&n.data.transition?(n.data.show=!0,r?$o(n,(function(){e.style.display=e.__vOriginalDisplay})):Eo(n,(function(){e.style.display="none"}))):e.style.display=r?e.__vOriginalDisplay:"none")},unbind:function(e,t,n,r,o){o||(e.style.display=e.__vOriginalDisplay)}}},Ho={name:String,appear:Boolean,css:Boolean,mode:String,type:String,enterClass:String,leaveClass:String,enterToClass:String,leaveToClass:String,enterActiveClass:String,leaveActiveClass:String,appearClass:String,appearActiveClass:String,appearToClass:String,duration:[Number,String,Object]};function Vo(e){var t=e&&e.componentOptions;return t&&t.Ctor.options.abstract?Vo(jt(t.children)):e}function Wo(e){var t={},n=e.$options;for(var r in n.propsData)t[r]=e[r];var o=n._parentListeners;for(var r in o)t[P(r)]=o[r];return t}function Ko(e,t){if(/\d-keep-alive$/.test(t.tag))return e("keep-alive",{props:t.componentOptions.propsData})}var Qo=function(e){return e.tag||dt(e)},Xo=function(e){return"show"===e.name},Jo={name:"transition",props:Ho,abstract:!0,render:function(e){var t=this,n=this.$slots.default;if(n&&(n=n.filter(Qo)).length){0;var r=this.mode;0;var o=n[0];if(function(e){for(;e=e.parent;)if(e.data.transition)return!0}(this.$vnode))return o;var i=Vo(o);if(!i)return o;if(this._leaving)return Ko(e,o);var a="__transition-".concat(this._uid,"-");i.key=null==i.key?i.isComment?a+"comment":a+i.tag:l(i.key)?0===String(i.key).indexOf(a)?i.key:a+i.key:i.key;var s=(i.data||(i.data={})).transition=Wo(this),u=this._vnode,c=Vo(u);if(i.data.directives&&i.data.directives.some(Xo)&&(i.data.show=!0),c&&c.data&&!function(e,t){return t.key===e.key&&t.tag===e.tag}(i,c)&&!dt(c)&&(!c.componentInstance||!c.componentInstance._vnode.isComment)){var f=c.data.transition=A({},s);if("out-in"===r)return this._leaving=!0,We(f,"afterLeave",(function(){t._leaving=!1,t.$forceUpdate()})),Ko(e,o);if("in-out"===r){if(dt(i))return u;var p,h=function(){p()};We(s,"afterEnter",h),We(s,"enterCancelled",h),We(f,"delayLeave",(function(e){p=e}))}}return o}}},Yo=A({tag:String,moveClass:String},Ho);function Zo(e){e.elm._moveCb&&e.elm._moveCb(),e.elm._enterCb&&e.elm._enterCb()}function ei(e){e.data.newPos=e.elm.getBoundingClientRect()}function ti(e){var t=e.data.pos,n=e.data.newPos,r=t.left-n.left,o=t.top-n.top;if(r||o){e.data.moved=!0;var i=e.elm.style;i.transform=i.WebkitTransform="translate(".concat(r,"px,").concat(o,"px)"),i.transitionDuration="0s"}}delete Yo.mode;var ni={Transition:Jo,TransitionGroup:{props:Yo,beforeMount:function(){var e=this,t=this._update;this._update=function(n,r){var o=Jt(e);e.__patch__(e._vnode,e.kept,!1,!0),e._vnode=e.kept,o(),t.call(e,n,r)}},render:function(e){for(var t=this.tag||this.$vnode.data.tag||"span",n=Object.create(null),r=this.prevChildren=this.children,o=this.$slots.default||[],i=this.children=[],a=Wo(this),s=0;s-1?yr[e]=t.constructor===window.HTMLUnknownElement||t.constructor===window.HTMLElement:yr[e]=/HTMLUnknownElement/.test(t.toString())},A(Wn.options.directives,Bo),A(Wn.options.components,ni),Wn.prototype.__patch__=Q?Lo:L,Wn.prototype.$mount=function(e,t){return function(e,t,n){var r;e.$el=t,e.$options.render||(e.$options.render=de),en(e,"beforeMount"),r=function(){e._update(e._render(),n)},new Ht(e,r,L,{before:function(){e._isMounted&&!e._isDestroyed&&en(e,"beforeUpdate")}},!0),n=!1;var o=e._preWatchers;if(o)for(var i=0;i=0&&(t=e.slice(r),e=e.slice(0,r));var o=e.indexOf("?");return o>=0&&(n=e.slice(o+1),e=e.slice(0,o)),{path:e,query:n,hash:t}}(o.path||""),u=t&&t.path||"/",c=l.path?ki(l.path,u,n||o.append):u,f=function(e,t,n){void 0===t&&(t={});var r,o=n||ci;try{r=o(e||"")}catch(e){r={}}for(var i in t){var a=t[i];r[i]=Array.isArray(a)?a.map(ui):ui(a)}return r}(l.query,o.query,r&&r.options.parseQuery),p=o.hash||l.hash;return p&&"#"!==p.charAt(0)&&(p="#"+p),{_normalized:!0,path:c,query:f,hash:p}}var Bi,Hi=function(){},Vi={name:"RouterLink",props:{to:{type:[String,Object],required:!0},tag:{type:String,default:"a"},custom:Boolean,exact:Boolean,exactPath:Boolean,append:Boolean,replace:Boolean,activeClass:String,exactActiveClass:String,ariaCurrentValue:{type:String,default:"page"},event:{type:[String,Array],default:"click"}},render:function(e){var t=this,n=this.$router,r=this.$route,o=n.resolve(this.to,r,this.append),i=o.location,a=o.route,s=o.href,l={},u=n.options.linkActiveClass,c=n.options.linkExactActiveClass,f=null==u?"router-link-active":u,p=null==c?"router-link-exact-active":c,h=null==this.activeClass?f:this.activeClass,d=null==this.exactActiveClass?p:this.exactActiveClass,v=a.redirectedFrom?hi(null,Ui(a.redirectedFrom),null,n):a;l[d]=yi(r,v,this.exactPath),l[h]=this.exact||this.exactPath?l[d]:function(e,t){return 0===e.path.replace(pi,"/").indexOf(t.path.replace(pi,"/"))&&(!t.hash||e.hash===t.hash)&&function(e,t){for(var n in t)if(!(n in e))return!1;return!0}(e.query,t.query)}(r,v);var g=l[d]?this.ariaCurrentValue:null,m=function(e){Wi(e)&&(t.replace?n.replace(i,Hi):n.push(i,Hi))},y={click:Wi};Array.isArray(this.event)?this.event.forEach((function(e){y[e]=m})):y[this.event]=m;var b={class:l},_=!this.$scopedSlots.$hasNormal&&this.$scopedSlots.default&&this.$scopedSlots.default({href:s,route:a,navigate:m,isActive:l[h],isExactActive:l[d]});if(_){if(1===_.length)return _[0];if(_.length>1||!_.length)return 0===_.length?e():e("span",{},_)}if("a"===this.tag)b.on=y,b.attrs={href:s,"aria-current":g};else{var x=function e(t){var n;if(t)for(var r=0;r-1&&(s.params[p]=n.params[p]);return s.path=Fi(c.path,s.params),l(c,s,a)}if(s.path){s.params={};for(var h=0;h-1}function ja(e,t){return ka(e)&&e._isRouter&&(null==t||e.type===t)}function Ca(e,t,n){var r=function(o){o>=e.length?n():e[o]?t(e[o],(function(){r(o+1)})):r(o+1)};r(0)}function Pa(e){return function(t,n,r){var o=!1,i=0,a=null;Sa(e,(function(e,t,n,s){if("function"==typeof e&&void 0===e.cid){o=!0,i++;var l,u=Ea((function(t){var o;((o=t).__esModule||$a&&"Module"===o[Symbol.toStringTag])&&(t=t.default),e.resolved="function"==typeof t?t:Bi.extend(t),n.components[s]=t,--i<=0&&r()})),c=Ea((function(e){var t="Failed to resolve async component "+s+": "+e;a||(a=ka(e)?e:new Error(t),r(a))}));try{l=e(u,c)}catch(e){c(e)}if(l)if("function"==typeof l.then)l.then(u,c);else{var f=l.component;f&&"function"==typeof f.then&&f.then(u,c)}}})),o||r()}}function Sa(e,t){return Oa(e.map((function(e){return Object.keys(e.components).map((function(n){return t(e.components[n],e.instances[n],e,n)}))})))}function Oa(e){return Array.prototype.concat.apply([],e)}var $a="function"==typeof Symbol&&"symbol"==typeof Symbol.toStringTag;function Ea(e){var t=!1;return function(){for(var n=[],r=arguments.length;r--;)n[r]=arguments[r];if(!t)return t=!0,e.apply(this,n)}}var Ta=function(e,t){this.router=e,this.base=function(e){if(!e)if(Ki){var t=document.querySelector("base");e=(e=t&&t.getAttribute("href")||"/").replace(/^https?:\/\/[^\/]+/,"")}else e="/";"/"!==e.charAt(0)&&(e="/"+e);return e.replace(/\/$/,"")}(t),this.current=vi,this.pending=null,this.ready=!1,this.readyCbs=[],this.readyErrorCbs=[],this.errorCbs=[],this.listeners=[]};function Aa(e,t,n,r){var o=Sa(e,(function(e,r,o,i){var a=function(e,t){"function"!=typeof e&&(e=Bi.extend(e));return e.options[t]}(e,t);if(a)return Array.isArray(a)?a.map((function(e){return n(e,r,o,i)})):n(a,r,o,i)}));return Oa(r?o.reverse():o)}function za(e,t){if(t)return function(){return e.apply(t,arguments)}}Ta.prototype.listen=function(e){this.cb=e},Ta.prototype.onReady=function(e,t){this.ready?e():(this.readyCbs.push(e),t&&this.readyErrorCbs.push(t))},Ta.prototype.onError=function(e){this.errorCbs.push(e)},Ta.prototype.transitionTo=function(e,t,n){var r,o=this;try{r=this.router.match(e,this.current)}catch(e){throw this.errorCbs.forEach((function(t){t(e)})),e}var i=this.current;this.confirmTransition(r,(function(){o.updateRoute(r),t&&t(r),o.ensureURL(),o.router.afterHooks.forEach((function(e){e&&e(r,i)})),o.ready||(o.ready=!0,o.readyCbs.forEach((function(e){e(r)})))}),(function(e){n&&n(e),e&&!o.ready&&(ja(e,ya.redirected)&&i===vi||(o.ready=!0,o.readyErrorCbs.forEach((function(t){t(e)}))))}))},Ta.prototype.confirmTransition=function(e,t,n){var r=this,o=this.current;this.pending=e;var i,a,s=function(e){!ja(e)&&ka(e)&&(r.errorCbs.length?r.errorCbs.forEach((function(t){t(e)})):console.error(e)),n&&n(e)},l=e.matched.length-1,u=o.matched.length-1;if(yi(e,o)&&l===u&&e.matched[l]===o.matched[u])return this.ensureURL(),e.hash&&aa(this.router,o,e,!1),s(((a=xa(i=o,e,ya.duplicated,'Avoided redundant navigation to current location: "'+i.fullPath+'".')).name="NavigationDuplicated",a));var c=function(e,t){var n,r=Math.max(e.length,t.length);for(n=0;n0)){var t=this.router,n=t.options.scrollBehavior,r=va&&n;r&&this.listeners.push(ia());var o=function(){var n=e.current,o=Ia(e.base);e.current===vi&&o===e._startLocation||e.transitionTo(o,(function(e){r&&aa(t,e,n,!0)}))};window.addEventListener("popstate",o),this.listeners.push((function(){window.removeEventListener("popstate",o)}))}},t.prototype.go=function(e){window.history.go(e)},t.prototype.push=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){ga(ji(r.base+e.fullPath)),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){ma(ji(r.base+e.fullPath)),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.ensureURL=function(e){if(Ia(this.base)!==this.current.fullPath){var t=ji(this.base+this.current.fullPath);e?ga(t):ma(t)}},t.prototype.getCurrentLocation=function(){return Ia(this.base)},t}(Ta);function Ia(e){var t=window.location.pathname,n=t.toLowerCase(),r=e.toLowerCase();return!e||n!==r&&0!==n.indexOf(ji(r+"/"))||(t=t.slice(e.length)),(t||"/")+window.location.search+window.location.hash}var Ra=function(e){function t(t,n,r){e.call(this,t,n),r&&function(e){var t=Ia(e);if(!/^\/#/.test(t))return window.location.replace(ji(e+"/#"+t)),!0}(this.base)||Ma()}return e&&(t.__proto__=e),t.prototype=Object.create(e&&e.prototype),t.prototype.constructor=t,t.prototype.setupListeners=function(){var e=this;if(!(this.listeners.length>0)){var t=this.router.options.scrollBehavior,n=va&&t;n&&this.listeners.push(ia());var r=function(){var t=e.current;Ma()&&e.transitionTo(Da(),(function(r){n&&aa(e.router,r,t,!0),va||Na(r.fullPath)}))},o=va?"popstate":"hashchange";window.addEventListener(o,r),this.listeners.push((function(){window.removeEventListener(o,r)}))}},t.prototype.push=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){Ga(e.fullPath),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this,o=this.current;this.transitionTo(e,(function(e){Na(e.fullPath),aa(r.router,e,o,!1),t&&t(e)}),n)},t.prototype.go=function(e){window.history.go(e)},t.prototype.ensureURL=function(e){var t=this.current.fullPath;Da()!==t&&(e?Ga(t):Na(t))},t.prototype.getCurrentLocation=function(){return Da()},t}(Ta);function Ma(){var e=Da();return"/"===e.charAt(0)||(Na("/"+e),!1)}function Da(){var e=window.location.href,t=e.indexOf("#");return t<0?"":e=e.slice(t+1)}function qa(e){var t=window.location.href,n=t.indexOf("#");return(n>=0?t.slice(0,n):t)+"#"+e}function Ga(e){va?ga(qa(e)):window.location.hash=e}function Na(e){va?ma(qa(e)):window.location.replace(qa(e))}var Fa=function(e){function t(t,n){e.call(this,t,n),this.stack=[],this.index=-1}return e&&(t.__proto__=e),t.prototype=Object.create(e&&e.prototype),t.prototype.constructor=t,t.prototype.push=function(e,t,n){var r=this;this.transitionTo(e,(function(e){r.stack=r.stack.slice(0,r.index+1).concat(e),r.index++,t&&t(e)}),n)},t.prototype.replace=function(e,t,n){var r=this;this.transitionTo(e,(function(e){r.stack=r.stack.slice(0,r.index).concat(e),t&&t(e)}),n)},t.prototype.go=function(e){var t=this,n=this.index+e;if(!(n<0||n>=this.stack.length)){var r=this.stack[n];this.confirmTransition(r,(function(){var e=t.current;t.index=n,t.updateRoute(r),t.router.afterHooks.forEach((function(t){t&&t(r,e)}))}),(function(e){ja(e,ya.duplicated)&&(t.index=n)}))}},t.prototype.getCurrentLocation=function(){var e=this.stack[this.stack.length-1];return e?e.fullPath:"/"},t.prototype.ensureURL=function(){},t}(Ta),Ua=function(e){void 0===e&&(e={}),this.app=null,this.apps=[],this.options=e,this.beforeHooks=[],this.resolveHooks=[],this.afterHooks=[],this.matcher=Ji(e.routes||[],this);var t=e.mode||"hash";switch(this.fallback="history"===t&&!va&&!1!==e.fallback,this.fallback&&(t="hash"),Ki||(t="abstract"),this.mode=t,t){case"history":this.history=new La(this,e.base);break;case"hash":this.history=new Ra(this,e.base,this.fallback);break;case"abstract":this.history=new Fa(this,e.base);break;default:0}},Ba={currentRoute:{configurable:!0}};Ua.prototype.match=function(e,t,n){return this.matcher.match(e,t,n)},Ba.currentRoute.get=function(){return this.history&&this.history.current},Ua.prototype.init=function(e){var t=this;if(this.apps.push(e),e.$once("hook:destroyed",(function(){var n=t.apps.indexOf(e);n>-1&&t.apps.splice(n,1),t.app===e&&(t.app=t.apps[0]||null),t.app||t.history.teardown()})),!this.app){this.app=e;var n=this.history;if(n instanceof La||n instanceof Ra){var r=function(e){n.setupListeners(),function(e){var r=n.current,o=t.options.scrollBehavior;va&&o&&"fullPath"in e&&aa(t,e,r,!1)}(e)};n.transitionTo(n.getCurrentLocation(),r,r)}n.listen((function(e){t.apps.forEach((function(t){t._route=e}))}))}},Ua.prototype.beforeEach=function(e){return Va(this.beforeHooks,e)},Ua.prototype.beforeResolve=function(e){return Va(this.resolveHooks,e)},Ua.prototype.afterEach=function(e){return Va(this.afterHooks,e)},Ua.prototype.onReady=function(e,t){this.history.onReady(e,t)},Ua.prototype.onError=function(e){this.history.onError(e)},Ua.prototype.push=function(e,t,n){var r=this;if(!t&&!n&&"undefined"!=typeof Promise)return new Promise((function(t,n){r.history.push(e,t,n)}));this.history.push(e,t,n)},Ua.prototype.replace=function(e,t,n){var r=this;if(!t&&!n&&"undefined"!=typeof Promise)return new Promise((function(t,n){r.history.replace(e,t,n)}));this.history.replace(e,t,n)},Ua.prototype.go=function(e){this.history.go(e)},Ua.prototype.back=function(){this.go(-1)},Ua.prototype.forward=function(){this.go(1)},Ua.prototype.getMatchedComponents=function(e){var t=e?e.matched?e:this.resolve(e).route:this.currentRoute;return t?[].concat.apply([],t.matched.map((function(e){return Object.keys(e.components).map((function(t){return e.components[t]}))}))):[]},Ua.prototype.resolve=function(e,t,n){var r=Ui(e,t=t||this.history.current,n,this),o=this.match(r,t),i=o.redirectedFrom||o.fullPath;return{location:r,route:o,href:function(e,t,n){var r="hash"===n?"#"+t:t;return e?ji(e+"/"+r):r}(this.history.base,i,this.mode),normalizedTo:r,resolved:o}},Ua.prototype.getRoutes=function(){return this.matcher.getRoutes()},Ua.prototype.addRoute=function(e,t){this.matcher.addRoute(e,t),this.history.current!==vi&&this.history.transitionTo(this.history.getCurrentLocation())},Ua.prototype.addRoutes=function(e){this.matcher.addRoutes(e),this.history.current!==vi&&this.history.transitionTo(this.history.getCurrentLocation())},Object.defineProperties(Ua.prototype,Ba);var Ha=Ua;function Va(e,t){return e.push(t),function(){var n=e.indexOf(t);n>-1&&e.splice(n,1)}}Ua.install=function e(t){if(!e.installed||Bi!==t){e.installed=!0,Bi=t;var n=function(e){return void 0!==e},r=function(e,t){var r=e.$options._parentVnode;n(r)&&n(r=r.data)&&n(r=r.registerRouteInstance)&&r(e,t)};t.mixin({beforeCreate:function(){n(this.$options.router)?(this._routerRoot=this,this._router=this.$options.router,this._router.init(this),t.util.defineReactive(this,"_route",this._router.history.current)):this._routerRoot=this.$parent&&this.$parent._routerRoot||this,r(this,this)},destroyed:function(){r(this)}}),Object.defineProperty(t.prototype,"$router",{get:function(){return this._routerRoot._router}}),Object.defineProperty(t.prototype,"$route",{get:function(){return this._routerRoot._route}}),t.component("RouterView",xi),t.component("RouterLink",Vi);var o=t.config.optionMergeStrategies;o.beforeRouteEnter=o.beforeRouteLeave=o.beforeRouteUpdate=o.created}},Ua.version="3.6.5",Ua.isNavigationFailure=ja,Ua.NavigationFailureType=ya,Ua.START_LOCATION=vi,Ki&&window.Vue&&window.Vue.use(Ua);n(98);n(125),n(91);var Wa={"components/AlgoliaSearchBox":()=>Promise.all([n.e(0),n.e(13)]).then(n.bind(null,307)),"components/DropdownLink":()=>Promise.all([n.e(0),n.e(14)]).then(n.bind(null,257)),"components/DropdownTransition":()=>Promise.all([n.e(0),n.e(21)]).then(n.bind(null,245)),"components/Home":()=>Promise.all([n.e(0),n.e(16)]).then(n.bind(null,288)),"components/NavLink":()=>n.e(26).then(n.bind(null,244)),"components/NavLinks":()=>Promise.all([n.e(0),n.e(12)]).then(n.bind(null,270)),"components/Navbar":()=>Promise.all([n.e(0),n.e(1)]).then(n.bind(null,303)),"components/Page":()=>Promise.all([n.e(0),n.e(11)]).then(n.bind(null,289)),"components/PageEdit":()=>Promise.all([n.e(0),n.e(17)]).then(n.bind(null,272)),"components/PageNav":()=>Promise.all([n.e(0),n.e(15)]).then(n.bind(null,273)),"components/Sidebar":()=>Promise.all([n.e(0),n.e(10)]).then(n.bind(null,290)),"components/SidebarButton":()=>Promise.all([n.e(0),n.e(22)]).then(n.bind(null,291)),"components/SidebarGroup":()=>Promise.all([n.e(0),n.e(3)]).then(n.bind(null,271)),"components/SidebarLink":()=>Promise.all([n.e(0),n.e(18)]).then(n.bind(null,258)),"components/SidebarLinks":()=>Promise.all([n.e(0),n.e(3)]).then(n.bind(null,256)),"global-components/Badge":()=>Promise.all([n.e(0),n.e(4)]).then(n.bind(null,312)),"global-components/CodeBlock":()=>Promise.all([n.e(0),n.e(5)]).then(n.bind(null,308)),"global-components/CodeGroup":()=>Promise.all([n.e(0),n.e(6)]).then(n.bind(null,309)),"layouts/404":()=>n.e(7).then(n.bind(null,310)),"layouts/Layout":()=>Promise.all([n.e(0),n.e(1),n.e(2)]).then(n.bind(null,311)),NotFound:()=>n.e(7).then(n.bind(null,310)),Layout:()=>Promise.all([n.e(0),n.e(1),n.e(2)]).then(n.bind(null,311))},Ka={"v-69ce29c7":()=>n.e(31).then(n.bind(null,313)),"v-04e5fff4":()=>n.e(28).then(n.bind(null,314)),"v-9e172538":()=>n.e(29).then(n.bind(null,315)),"v-4fc17f5a":()=>n.e(30).then(n.bind(null,316)),"v-c546ebae":()=>n.e(32).then(n.bind(null,317)),"v-8d322532":()=>n.e(23).then(n.bind(null,318)),"v-000e4fc7":()=>n.e(34).then(n.bind(null,319)),"v-a6bb140e":()=>n.e(33).then(n.bind(null,320)),"v-41e35097":()=>n.e(19).then(n.bind(null,321)),"v-37bab107":()=>n.e(35).then(n.bind(null,322)),"v-b433d65a":()=>n.e(38).then(n.bind(null,323)),"v-388cef95":()=>n.e(36).then(n.bind(null,324)),"v-0fa3efe4":()=>n.e(41).then(n.bind(null,325)),"v-adba83b2":()=>n.e(39).then(n.bind(null,326)),"v-a95f94f2":()=>n.e(24).then(n.bind(null,327)),"v-de7fe9d6":()=>n.e(42).then(n.bind(null,328)),"v-66982838":()=>n.e(43).then(n.bind(null,329)),"v-64349095":()=>n.e(44).then(n.bind(null,330)),"v-30ff3e89":()=>n.e(40).then(n.bind(null,331)),"v-e4b26f16":()=>n.e(45).then(n.bind(null,332)),"v-444dc367":()=>n.e(27).then(n.bind(null,333)),"v-648f41b8":()=>n.e(47).then(n.bind(null,334)),"v-52076f24":()=>n.e(46).then(n.bind(null,335)),"v-172539b2":()=>n.e(37).then(n.bind(null,336)),"v-59b78160":()=>n.e(48).then(n.bind(null,337)),"v-9a95a632":()=>n.e(51).then(n.bind(null,338)),"v-4700bfe7":()=>n.e(52).then(n.bind(null,339)),"v-59bfc02b":()=>n.e(53).then(n.bind(null,340)),"v-2e51fb11":()=>n.e(54).then(n.bind(null,341)),"v-30329847":()=>n.e(50).then(n.bind(null,342)),"v-123e70c3":()=>n.e(57).then(n.bind(null,343)),"v-e8628a72":()=>n.e(20).then(n.bind(null,344)),"v-7c7b70db":()=>n.e(56).then(n.bind(null,345)),"v-6a7c97c5":()=>n.e(59).then(n.bind(null,346)),"v-d6a675f2":()=>n.e(55).then(n.bind(null,347)),"v-555fc7c7":()=>n.e(60).then(n.bind(null,348)),"v-71628cc5":()=>n.e(62).then(n.bind(null,349)),"v-46a3f904":()=>n.e(49).then(n.bind(null,350)),"v-9162d5b2":()=>n.e(58).then(n.bind(null,351)),"v-3f115d44":()=>n.e(64).then(n.bind(null,352)),"v-36b8ee32":()=>n.e(63).then(n.bind(null,353)),"v-3a551ac7":()=>n.e(61).then(n.bind(null,354)),"v-7e64f407":()=>n.e(68).then(n.bind(null,355)),"v-e844302a":()=>n.e(25).then(n.bind(null,356)),"v-0f448959":()=>n.e(65).then(n.bind(null,357)),"v-bce8af32":()=>n.e(66).then(n.bind(null,358)),"v-4964c3a7":()=>n.e(67).then(n.bind(null,359)),"v-347d4327":()=>n.e(70).then(n.bind(null,360)),"v-735b4bc7":()=>n.e(72).then(n.bind(null,361)),"v-3c133eb2":()=>n.e(71).then(n.bind(null,362)),"v-40feb13a":()=>n.e(73).then(n.bind(null,363)),"v-7f6e5700":()=>n.e(69).then(n.bind(null,364))};function Qa(e){const t=Object.create(null);return function(n){return t[n]||(t[n]=e(n))}}const Xa=/-(\w)/g,Ja=Qa(e=>e.replace(Xa,(e,t)=>t?t.toUpperCase():"")),Ya=/\B([A-Z])/g,Za=Qa(e=>e.replace(Ya,"-$1").toLowerCase()),es=Qa(e=>e.charAt(0).toUpperCase()+e.slice(1));function ts(e,t){if(!t)return;if(e(t))return e(t);return t.includes("-")?e(es(Ja(t))):e(es(t))||e(Za(t))}const ns=Object.assign({},Wa,Ka),rs=e=>ns[e],os=e=>Ka[e],is=e=>Wa[e],as=e=>Wn.component(e);function ss(e){return ts(os,e)}function ls(e){return ts(is,e)}function us(e){return ts(rs,e)}function cs(e){return ts(as,e)}function fs(...e){return Promise.all(e.filter(e=>e).map(async e=>{if(!cs(e)&&us(e)){const t=await us(e)();Wn.component(e,t.default)}}))}function ps(e,t){"undefined"!=typeof window&&window.__VUEPRESS__&&(window.__VUEPRESS__[e]=t)}var hs=n(88),ds=n.n(hs),vs=n(89),gs=n.n(vs),ms={created(){if(this.siteMeta=this.$site.headTags.filter(([e])=>"meta"===e).map(([e,t])=>t),this.$ssrContext){const t=this.getMergedMetaTags();this.$ssrContext.title=this.$title,this.$ssrContext.lang=this.$lang,this.$ssrContext.pageMeta=(e=t)?e.map(e=>{let t="{t+=` ${n}="${gs()(e[n])}"`}),t+">"}).join("\n "):"",this.$ssrContext.canonicalLink=bs(this.$canonicalUrl)}var e},mounted(){this.currentMetaTags=[...document.querySelectorAll("meta")],this.updateMeta(),this.updateCanonicalLink()},methods:{updateMeta(){document.title=this.$title,document.documentElement.lang=this.$lang;const e=this.getMergedMetaTags();this.currentMetaTags=_s(e,this.currentMetaTags)},getMergedMetaTags(){const e=this.$page.frontmatter.meta||[];return ds()([{name:"description",content:this.$description}],e,this.siteMeta,xs)},updateCanonicalLink(){ys(),this.$canonicalUrl&&document.head.insertAdjacentHTML("beforeend",bs(this.$canonicalUrl))}},watch:{$page(){this.updateMeta(),this.updateCanonicalLink()}},beforeDestroy(){_s(null,this.currentMetaTags),ys()}};function ys(){const e=document.querySelector("link[rel='canonical']");e&&e.remove()}function bs(e=""){return e?``:""}function _s(e,t){if(t&&[...t].filter(e=>e.parentNode===document.head).forEach(e=>document.head.removeChild(e)),e)return e.map(e=>{const t=document.createElement("meta");return Object.keys(e).forEach(n=>{t.setAttribute(n,e[n])}),document.head.appendChild(t),t})}function xs(e){for(const t of["name","property","itemprop"])if(e.hasOwnProperty(t))return e[t]+t;return JSON.stringify(e)}var ws=n(90),ks={mounted(){window.addEventListener("scroll",this.onScroll)},methods:{onScroll:n.n(ws)()((function(){this.setActiveHash()}),300),setActiveHash(){const e=[].slice.call(document.querySelectorAll(".sidebar-link")),t=[].slice.call(document.querySelectorAll(".header-anchor")).filter(t=>e.some(e=>e.hash===t.hash)),n=Math.max(window.pageYOffset,document.documentElement.scrollTop,document.body.scrollTop),r=Math.max(document.documentElement.scrollHeight,document.body.scrollHeight),o=window.innerHeight+n;for(let e=0;e=i.parentElement.offsetTop+10&&(!a||n{this.$nextTick(()=>{this.$vuepress.$set("disableScrollBehavior",!1)})})}}}},beforeDestroy(){window.removeEventListener("scroll",this.onScroll)}},js=n(22),Cs=n.n(js),Ps=[ms,ks,{mounted(){Cs.a.configure({showSpinner:!1}),this.$router.beforeEach((e,t,n)=>{e.path===t.path||Wn.component(e.name)||Cs.a.start(),n()}),this.$router.afterEach(()=>{Cs.a.done(),this.isSidebarOpen=!1})}}],Ss={name:"GlobalLayout",computed:{layout(){const e=this.getLayout();return ps("layout",e),Wn.component(e)}},methods:{getLayout(){if(this.$page.path){const e=this.$page.frontmatter.layout;return e&&(this.$vuepress.getLayoutAsyncComponent(e)||this.$vuepress.getVueComponent(e))?e:"Layout"}return"NotFound"}}},Os=n(10),$s=Object(Os.a)(Ss,(function(){return(0,this._self._c)(this.layout,{tag:"component"})}),[],!1,null,null,null).exports;!function(e,t,n){switch(t){case"components":e[t]||(e[t]={}),Object.assign(e[t],n);break;case"mixins":e[t]||(e[t]=[]),e[t].push(...n);break;default:throw new Error("Unknown option name.")}}($s,"mixins",Ps);const Es=[{name:"v-69ce29c7",path:"/guide/auto-gen-priest.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-69ce29c7").then(n)}},{name:"v-04e5fff4",path:"/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-04e5fff4").then(n)}},{path:"/index.html",redirect:"/"},{name:"v-9e172538",path:"/goners/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9e172538").then(n)}},{path:"/goners/index.html",redirect:"/goners/"},{name:"v-4fc17f5a",path:"/guide/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4fc17f5a").then(n)}},{path:"/guide/index.html",redirect:"/guide/"},{name:"v-c546ebae",path:"/guide/config.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-c546ebae").then(n)}},{name:"v-8d322532",path:"/guide/gone-and-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-8d322532").then(n)}},{name:"v-000e4fc7",path:"/guide/hooks.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-000e4fc7").then(n)}},{name:"v-a6bb140e",path:"/guide/core-concept.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-a6bb140e").then(n)}},{name:"v-41e35097",path:"/guide/goner-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-41e35097").then(n)}},{name:"v-37bab107",path:"/guide/inner-goner.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-37bab107").then(n)}},{name:"v-b433d65a",path:"/guide/tracer.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-b433d65a").then(n)}},{name:"v-388cef95",path:"/guide/logrus.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-388cef95").then(n)}},{name:"v-0fa3efe4",path:"/quick-start/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-0fa3efe4").then(n)}},{path:"/quick-start/index.html",redirect:"/quick-start/"},{name:"v-adba83b2",path:"/guide/unit-test.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-adba83b2").then(n)}},{name:"v-a95f94f2",path:"/quick-start/mysql.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-a95f94f2").then(n)}},{name:"v-de7fe9d6",path:"/quick-start/simple.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-de7fe9d6").then(n)}},{name:"v-66982838",path:"/references/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-66982838").then(n)}},{path:"/references/index.html",redirect:"/references/"},{name:"v-64349095",path:"/references/gone-tool.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-64349095").then(n)}},{name:"v-30ff3e89",path:"/guide/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-30ff3e89").then(n)}},{name:"v-e4b26f16",path:"/references/http-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e4b26f16").then(n)}},{name:"v-444dc367",path:"/zh/blogs/1_v1.x_release.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-444dc367").then(n)}},{name:"v-648f41b8",path:"/zh/blogs/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-648f41b8").then(n)}},{path:"/zh/blogs/index.html",redirect:"/zh/blogs/"},{name:"v-52076f24",path:"/zh/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-52076f24").then(n)}},{path:"/zh/index.html",redirect:"/zh/"},{name:"v-172539b2",path:"/guide/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-172539b2").then(n)}},{name:"v-59b78160",path:"/zh/goners/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-59b78160").then(n)}},{path:"/zh/goners/index.html",redirect:"/zh/goners/"},{name:"v-9a95a632",path:"/zh/guide/config.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9a95a632").then(n)}},{name:"v-4700bfe7",path:"/zh/guide/core-concept.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4700bfe7").then(n)}},{name:"v-59bfc02b",path:"/zh/guide/error.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-59bfc02b").then(n)}},{name:"v-2e51fb11",path:"/zh/guide/gone-and-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-2e51fb11").then(n)}},{name:"v-30329847",path:"/zh/guide/auto-gen-priest.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-30329847").then(n)}},{name:"v-123e70c3",path:"/zh/guide/inner-goner.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-123e70c3").then(n)}},{name:"v-e8628a72",path:"/zh/guide/goner-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e8628a72").then(n)}},{name:"v-7c7b70db",path:"/zh/guide/hooks.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7c7b70db").then(n)}},{name:"v-6a7c97c5",path:"/zh/guide/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-6a7c97c5").then(n)}},{name:"v-d6a675f2",path:"/zh/guide/gone-vs-spring.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-d6a675f2").then(n)}},{name:"v-555fc7c7",path:"/zh/guide/schedule.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-555fc7c7").then(n)}},{name:"v-71628cc5",path:"/zh/guide/unit-test.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-71628cc5").then(n)}},{name:"v-46a3f904",path:"/zh/guide/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-46a3f904").then(n)}},{path:"/zh/guide/index.html",redirect:"/zh/guide/"},{name:"v-9162d5b2",path:"/zh/guide/logrus.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-9162d5b2").then(n)}},{name:"v-3f115d44",path:"/zh/quick-start/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3f115d44").then(n)}},{path:"/zh/quick-start/index.html",redirect:"/zh/quick-start/"},{name:"v-36b8ee32",path:"/zh/guide/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-36b8ee32").then(n)}},{name:"v-3a551ac7",path:"/zh/guide/tracer.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3a551ac7").then(n)}},{name:"v-7e64f407",path:"/zh/quick-start/web.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7e64f407").then(n)}},{name:"v-e844302a",path:"/zh/quick-start/mysql.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-e844302a").then(n)}},{name:"v-0f448959",path:"/zh/quick-start/gpt.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-0f448959").then(n)}},{name:"v-bce8af32",path:"/zh/quick-start/jwt+mysql+redis+schedule.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-bce8af32").then(n)}},{name:"v-4964c3a7",path:"/zh/quick-start/simple.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-4964c3a7").then(n)}},{name:"v-347d4327",path:"/zh/references/gone-tool.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-347d4327").then(n)}},{name:"v-735b4bc7",path:"/zh/references/redis.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-735b4bc7").then(n)}},{name:"v-3c133eb2",path:"/zh/references/http-inject.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-3c133eb2").then(n)}},{name:"v-40feb13a",path:"/zh/references/xorm.html",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-40feb13a").then(n)}},{name:"v-7f6e5700",path:"/zh/references/",component:$s,beforeEnter:(e,t,n)=>{fs("Layout","v-7f6e5700").then(n)}},{path:"/zh/references/index.html",redirect:"/zh/references/"},{path:"*",component:$s}],Ts={title:"",description:"",base:"/",headTags:[["link",{rel:"icon",href:"/assets/img/logo.png"}],["script",{type:"text/javascript"},"\n if (location.hostname != 'localhost'){\n var _hmt = _hmt || [];\n (function() {\n var hm = document.createElement(\"script\");\n hm.src = \"https://hm.baidu.com/hm.js?ed9e5a48d8eeb5e6b55a0de9cb8f6486\";\n var s = document.getElementsByTagName(\"script\")[0]; \n s.parentNode.insertBefore(hm, s);\n })();\n\n if(location.pathname == '/my-github'){\n location.href='https://github.com/gone-io/gone'\n }\n }\n "]],pages:[{title:"Auto-generate Priest",frontmatter:{sidebar:"auto",prev:"./tracer",next:"./redis"},regularPath:"/guide/auto-gen-priest.html",relativePath:"guide/auto-gen-priest.md",key:"v-69ce29c7",path:"/guide/auto-gen-priest.html",headers:[{level:2,title:"Getting Started",slug:"getting-started"},{level:3,title:"1. Install the gone auxiliary tool",slug:"_1-install-the-gone-auxiliary-tool"},{level:3,title:"2. Write the Goner",slug:"_2-write-the-goner"},{level:3,title:"3. Generate code",slug:"_3-generate-code"},{level:2,title:"Best Practices",slug:"best-practices"}]},{title:"Introduction",frontmatter:{sidebar:"auto",prev:!1,next:"./quick-start/"},regularPath:"/",relativePath:"README.md",key:"v-04e5fff4",path:"/",headers:[{level:2,title:"What is Gone?",slug:"what-is-gone"},{level:2,title:"Web Service",slug:"web-service"},{level:2,title:"Concepts",slug:"concepts"}]},{title:"Goners",frontmatter:{sidebar:"auto",prev:"../references/",next:"../"},regularPath:"/goners/",relativePath:"goners/README.md",key:"v-9e172538",path:"/goners/",headers:[{level:2,title:"Core-Level Goners",slug:"core-level-goners"},{level:2,title:"Built-in Framework Goners",slug:"built-in-framework-goners"},{level:2,title:"Ecosystem-Level Goners",slug:"ecosystem-level-goners"}]},{title:"Development Guide",frontmatter:{sidebar:"auto",prev:"../quick-start/",next:"../references/"},regularPath:"/guide/",relativePath:"guide/README.md",key:"v-4fc17f5a",path:"/guide/",headers:[{level:2,title:"Core Concepts of Gone",slug:"core-concepts-of-gone"},{level:2,title:"What Injection Methods Does Gone Support?",slug:"what-injection-methods-does-gone-support"},{level:2,title:"How to Gracefully Use Built-in Goners?",slug:"how-to-gracefully-use-built-in-goners"},{level:2,title:"Supporting Configuration Files via Built-in Goners",slug:"supporting-configuration-files-via-built-in-goners"},{level:2,title:"Supporting Database Connections via Built-in Goners",slug:"supporting-database-connections-via-built-in-goners"},{level:2,title:"Hook Functions",slug:"hook-functions"},{level:2,title:"Logging Output",slug:"logging-output"},{level:2,title:"Tracing Logs with TraceID",slug:"tracing-logs-with-traceid"},{level:2,title:"Auto-generating Priests",slug:"auto-generating-priests"}]},{title:"Configuring with Built-in Goners Support",frontmatter:{sidebar:"auto",prev:"./inner-goner",next:"./xorm"},regularPath:"/guide/config.html",relativePath:"guide/config.md",key:"v-c546ebae",path:"/guide/config.html",headers:[{level:2,title:"Example",slug:"example"},{level:3,title:"1. Create a mod",slug:"_1-create-a-mod"},{level:3,title:"2. Add a configuration file",slug:"_2-add-a-configuration-file"},{level:3,title:"3. Add code",slug:"_3-add-code"},{level:3,title:"4. Run",slug:"_4-run"},{level:2,title:"Configuration Injection",slug:"configuration-injection"},{level:3,title:"Tag Format Used",slug:"tag-format-used"},{level:3,title:"Supported Data Types for Injection",slug:"supported-data-types-for-injection"},{level:3,title:"Configuration Files",slug:"configuration-files"}]},{title:"Goner and Dependency Injection",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./goner-inject"},regularPath:"/guide/gone-and-inject.html",relativePath:"guide/gone-and-inject.md",key:"v-8d322532",path:"/guide/gone-and-inject.html",headers:[{level:2,title:"Goner and Dependency Injection",slug:"goner-and-dependency-injection"},{level:3,title:"Definition of Goner",slug:"definition-of-goner"},{level:3,title:"How Dependency Injection is Achieved in Gone",slug:"how-dependency-injection-is-achieved-in-gone"},{level:3,title:"How to Execute Business Code in Goners?",slug:"how-to-execute-business-code-in-goners"},{level:3,title:"The gone Command: Automatically Generating Priest Functions",slug:"the-gone-command-automatically-generating-priest-functions"},{level:2,title:"Anonymous Injection vs Named Injection",slug:"anonymous-injection-vs-named-injection"},{level:3,title:"Named Burial",slug:"named-burial"},{level:2,title:"Pointer Injection, Value Injection, and Interface Injection",slug:"pointer-injection-value-injection-and-interface-injection"},{level:3,title:"Pointer Injection vs Interface Injection",slug:"pointer-injection-vs-interface-injection"},{level:2,title:"Slice Injection and Map Injection",slug:"slice-injection-and-map-injection"},{level:3,title:"Private Property Injection",slug:"private-property-injection"},{level:2,title:"Configuration Injection",slug:"configuration-injection"}]},{title:"Gone's Hook Functions",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./logrus"},regularPath:"/guide/hooks.html",relativePath:"guide/hooks.md",key:"v-000e4fc7",path:"/guide/hooks.html",headers:[{level:2,title:"What are Hook Functions?",slug:"what-are-hook-functions"},{level:2,title:"Code Example",slug:"code-example"},{level:3,title:"Rules for Hook Functions",slug:"rules-for-hook-functions"},{level:2,title:"Gone's Startup Process",slug:"gone-s-startup-process"}]},{title:"Core Concepts of Gone",frontmatter:{sidebar:"auto",prev:!1,next:"./gone-and-inject"},regularPath:"/guide/core-concept.html",relativePath:"guide/core-concept.md",key:"v-a6bb140e",path:"/guide/core-concept.html",headers:[{level:2,title:"Goner",slug:"goner"},{level:3,title:"🔮 Prophet",slug:"🔮-prophet"},{level:3,title:"😇 Angel",slug:"😇-angel"},{level:3,title:"🧛🏻‍♀️ Vampire",slug:"🧛🏻‍♀️-vampire"},{level:2,title:"Cemetery",slug:"cemetery"},{level:3,title:"Burying",slug:"burying"},{level:3,title:"Reviving",slug:"reviving"},{level:2,title:"Heaven",slug:"heaven"},{level:2,title:"Priest",slug:"priest"}]},{title:"Injection Methods Supported by Gone",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./inner-goner"},regularPath:"/guide/goner-inject.html",relativePath:"guide/goner-inject.md",key:"v-41e35097",path:"/guide/goner-inject.html",headers:[{level:2,title:"Public Property Injection vs Private Property Injection",slug:"public-property-injection-vs-private-property-injection"},{level:2,title:"Anonymous Injection vs Named Injection",slug:"anonymous-injection-vs-named-injection"},{level:3,title:"Anonymous Burial and Named Burial",slug:"anonymous-burial-and-named-burial"},{level:3,title:"Anonymous Injection",slug:"anonymous-injection"},{level:3,title:"Named Injection",slug:"named-injection"},{level:2,title:"Supported Injection Property Types",slug:"supported-injection-property-types"},{level:3,title:"1. Pointer Types",slug:"_1-pointer-types"},{level:3,title:"2. Value Types",slug:"_2-value-types"},{level:3,title:"3. Interface Types",slug:"_3-interface-types"},{level:3,title:"4. Slice Types",slug:"_4-slice-types"},{level:3,title:"5. Map Types",slug:"_5-map-types"},{level:2,title:"Pointer Injection vs Interface Injection",slug:"pointer-injection-vs-interface-injection"},{level:2,title:"Slice Injection vs Map Injection",slug:"slice-injection-vs-map-injection"}]},{title:"How to Gracefully Use Built-in Goners?",frontmatter:{sidebar:"auto",prev:"./goner-inject",next:"./config"},regularPath:"/guide/inner-goner.html",relativePath:"guide/inner-goner.md",key:"v-37bab107",path:"/guide/inner-goner.html",headers:[{level:2,title:"Built-in Goners",slug:"built-in-goners"},{level:2,title:"The Priest Function",slug:"the-priest-function"},{level:2,title:"How to Use",slug:"how-to-use"},{level:2,title:"GoneId for Built-in Goners",slug:"goneid-for-built-in-goners"}]},{title:"Trace Log with traceId",frontmatter:{sidebar:"auto",prev:"./logrus",next:"./auto-gen-priest"},regularPath:"/guide/tracer.html",relativePath:"guide/tracer.md",key:"v-b433d65a",path:"/guide/tracer.html",headers:[{level:2,title:"Burying Related Goners in the Cemetery",slug:"burying-related-goners-in-the-cemetery"},{level:2,title:"Simple Usage",slug:"simple-usage"},{level:2,title:"Passing TraceId Across Goroutines",slug:"passing-traceid-across-goroutines"},{level:2,title:"Passing TraceId Across Processes/Services",slug:"passing-traceid-across-processes-services"},{level:2,title:"Multi-language Support",slug:"multi-language-support"}]},{title:"Logging Output",frontmatter:{sidebar:"auto",prev:"./hooks",next:"./tracer"},regularPath:"/guide/logrus.html",relativePath:"guide/logrus.md",key:"v-388cef95",path:"/guide/logrus.html",headers:[{level:2,title:"Burying Related Goners in the Cemetery",slug:"burying-related-goners-in-the-cemetery"},{level:2,title:"Adding Related Configurations in the Configuration File",slug:"adding-related-configurations-in-the-configuration-file"},{level:2,title:"Logging",slug:"logging"},{level:2,title:"Log Format",slug:"log-format"},{level:2,title:"About TraceId",slug:"about-traceid"}]},{title:"Quick Start",frontmatter:{sidebar:"auto",prev:"../",next:"../guide/"},regularPath:"/quick-start/",relativePath:"quick-start/README.md",key:"v-0fa3efe4",path:"/quick-start/",headers:[{level:2,title:"Create a Simple Web Project",slug:"create-a-simple-web-project"},{level:2,title:"Create a Production-Ready Project Using MySQL",slug:"create-a-production-ready-project-using-mysql"},{level:2,title:"More Examples",slug:"more-examples"}]},{title:"Unit Testing",frontmatter:{sidebar:"auto",prev:"./redis",next:!1},regularPath:"/guide/unit-test.html",relativePath:"guide/unit-test.md",key:"v-adba83b2",path:"/guide/unit-test.html",headers:[{level:2,title:"Assume Our Written Goner is as Follows",slug:"assume-our-written-goner-is-as-follows"},{level:2,title:"We Can Write a Test File as Follows:",slug:"we-can-write-a-test-file-as-follows"},{level:2,title:"Using gomock for Mock Testing",slug:"using-gomock-for-mock-testing"}]},{title:"Web + MySQL",frontmatter:{sidebar:"auto",prev:"./simple",next:!1},regularPath:"/quick-start/mysql.html",relativePath:"quick-start/mysql.md",key:"v-a95f94f2",path:"/quick-start/mysql.html",headers:[{level:2,title:"Install the gone Tool",slug:"install-the-gone-tool"},{level:2,title:"Create a Project Using the gone Command",slug:"create-a-project-using-the-gone-command"},{level:2,title:"Compile And Run",slug:"compile-and-run"},{level:2,title:"API Testing",slug:"api-testing"},{level:2,title:"Project Brief Introduction",slug:"project-brief-introduction"},{level:3,title:"Directory Structure",slug:"directory-structure"},{level:3,title:"Project Features",slug:"project-features"},{level:3,title:"Partial Code Explanation",slug:"partial-code-explanation"},{level:3,title:"HTTP Injection",slug:"http-injection"},{level:3,title:"Request Handling Function Parameters",slug:"request-handling-function-parameters"}]},{title:"Creating a Simple Web Project",frontmatter:{sidebar:"auto",prev:!1,next:"./mysql"},regularPath:"/quick-start/simple.html",relativePath:"quick-start/simple.md",key:"v-de7fe9d6",path:"/quick-start/simple.html",headers:[{level:2,title:"Installing the Gone Utility Tool",slug:"installing-the-gone-utility-tool"},{level:2,title:"Creating a Web Project and Running the Code",slug:"creating-a-web-project-and-running-the-code"},{level:2,title:"Project Structure",slug:"project-structure"},{level:2,title:"Router",slug:"router"},{level:2,title:"Controller",slug:"controller"},{level:2,title:"Service",slug:"service"},{level:2,title:"Use Database",slug:"use-database"}]},{title:"Reference Documentation",frontmatter:{sidebar:"auto",prev:"../guide/",next:"../goners/"},regularPath:"/references/",relativePath:"references/README.md",key:"v-66982838",path:"/references/",headers:[{level:2,title:"Gone Tool",slug:"gone-tool"},{level:2,title:"HTTP Injection Instructions",slug:"http-injection-instructions"}]},{title:"Gone Tool",frontmatter:{sidebar:"auto",prev:!1,next:"./http-inject"},regularPath:"/references/gone-tool.html",relativePath:"references/gone-tool.md",key:"v-64349095",path:"/references/gone-tool.html",headers:[{level:2,title:"Installation",slug:"installation"},{level:2,title:"Usage",slug:"usage"},{level:3,title:"Testing Installation",slug:"testing-installation"},{level:3,title:"Generate Project",slug:"generate-project"},{level:3,title:"Generate Priest Function",slug:"generate-priest-function"}]},{title:"Supporting Database Operations with Built-in Goners",frontmatter:{sidebar:"auto",prev:"./config",next:"./hooks"},regularPath:"/guide/xorm.html",relativePath:"guide/xorm.md",key:"v-30ff3e89",path:"/guide/xorm.html",headers:[{level:2,title:"Example with MySQL",slug:"example-with-mysql"},{level:3,title:"1. Prepare MySQL Service",slug:"_1-prepare-mysql-service"},{level:3,title:"2. Create Module and Configuration",slug:"_2-create-module-and-configuration"},{level:3,title:"3. Write Code",slug:"_3-write-code"},{level:3,title:"4. Run the Code",slug:"_4-run-the-code"},{level:2,title:"Points to Note",slug:"points-to-note"},{level:3,title:"1. github.com/gone-io/gone/goner is a wrapper for xorm.io/xorm",slug:"_1-github-com-gone-io-gone-goner-is-a-wrapper-for-xorm-io-xorm"},{level:3,title:"2. Injection via gone-xorm GonerId",slug:"_2-injection-via-gone-xorm-gonerid"},{level:3,title:"3. Pay Attention to Imported Packages",slug:"_3-pay-attention-to-imported-packages"}]},{title:"HTTP Injection Instructions",frontmatter:{sidebar:"auto",prev:"./gone-tool",next:!1},regularPath:"/references/http-inject.html",relativePath:"references/http-inject.md",key:"v-e4b26f16",path:"/references/http-inject.html",headers:[{level:2,title:"Format of HTTP Dependency Injection Tags",slug:"format-of-http-dependency-injection-tags"},{level:2,title:"Supported Injection Types and Response Tags",slug:"supported-injection-types-and-response-tags"},{level:2,title:"Notes",slug:"notes"}]},{title:"v1.x版本 正式发布",frontmatter:{sidebar:"auto",prev:!1,next:!1},regularPath:"/zh/blogs/1_v1.x_release.html",relativePath:"zh/blogs/1_v1.x_release.md",key:"v-444dc367",path:"/zh/blogs/1_v1.x_release.html",headers:[{level:2,title:"优化和新特性",slug:"优化和新特性"},{level:3,title:"gone 核心功能增强",slug:"gone-核心功能增强"},{level:3,title:"内置Goners",slug:"内置goners"},{level:3,title:"覆盖测试",slug:"覆盖测试"},{level:2,title:"后续计划",slug:"后续计划"}]},{title:"博客",frontmatter:{sidebar:"auto",prev:!1,next:"./1_v1.x_release"},regularPath:"/zh/blogs/",relativePath:"zh/blogs/README.md",key:"v-648f41b8",path:"/zh/blogs/",headers:[{level:2,title:"v1.x版本 正式发布",slug:"v1-x版本-正式发布"}]},{title:"介绍",frontmatter:{sidebar:"auto",prev:!1,next:"./quick-start/"},regularPath:"/zh/",relativePath:"zh/README.md",key:"v-52076f24",path:"/zh/",headers:[{level:2,title:"Gone是什么",slug:"gone是什么"},{level:2,title:"Web服务",slug:"web服务"},{level:2,title:"概念",slug:"概念"}]},{title:"Using Redis for Distributed Locking and Caching",frontmatter:{sidebar:"auto",prev:"./auto-gen-priest",next:"./unit-test"},regularPath:"/guide/redis.html",relativePath:"guide/redis.md",key:"v-172539b2",path:"/guide/redis.html",headers:[{level:2,title:"Step 1: Embedding Redis Related Goner into Cemetery",slug:"step-1-embedding-redis-related-goner-into-cemetery"},{level:2,title:"Step 2: Adding Redis Configuration to the Configuration File",slug:"step-2-adding-redis-configuration-to-the-configuration-file"},{level:2,title:"Step 3: Using Redis",slug:"step-3-using-redis"},{level:3,title:"Injecting Interfaces",slug:"injecting-interfaces"},{level:3,title:"Using Distributed Cache",slug:"using-distributed-cache"},{level:3,title:"Using Distributed Lock",slug:"using-distributed-lock"},{level:2,title:"Complete Example Code",slug:"complete-example-code"}]},{title:"Goners",frontmatter:{sidebar:"auto",prev:"../references/",next:"../"},regularPath:"/zh/goners/",relativePath:"zh/goners/README.md",key:"v-59b78160",path:"/zh/goners/",headers:[{level:2,title:"核心级Goners",slug:"核心级goners"},{level:2,title:"框架内置Goners",slug:"框架内置goners"},{level:2,title:"生态级 Goners",slug:"生态级-goners"}]},{title:"通过内置Goners支持配置文件",frontmatter:{sidebar:"auto",prev:"./inner-goner",next:"./logrus"},regularPath:"/zh/guide/config.html",relativePath:"zh/guide/config.md",key:"v-9a95a632",path:"/zh/guide/config.html",headers:[{level:2,title:"举个例子",slug:"举个例子"},{level:3,title:"1. 创建mod",slug:"_1-创建mod"},{level:3,title:"2. 添加配置文件",slug:"_2-添加配置文件"},{level:3,title:"3. 添加代码",slug:"_3-添加代码"},{level:3,title:"4. 运行",slug:"_4-运行"},{level:2,title:"配置注入",slug:"配置注入"},{level:3,title:"使用的标签格式",slug:"使用的标签格式"},{level:3,title:"支持注入的数据类型",slug:"支持注入的数据类型"},{level:3,title:"配置文件",slug:"配置文件"}]},{title:"Gone的核心概念",frontmatter:{sidebar:"auto",prev:!1,next:"./gone-and-inject"},regularPath:"/zh/guide/core-concept.html",relativePath:"zh/guide/core-concept.md",key:"v-4700bfe7",path:"/zh/guide/core-concept.html",headers:[{level:2,title:"Goner(逝者)",slug:"goner-逝者"},{level:3,title:"🔮Prophet(先知)",slug:"🔮prophet-先知"},{level:3,title:"😇Angel(天使)",slug:"😇angel-天使"},{level:3,title:"🧛🏻‍♀️Vampire(吸血鬼)",slug:"🧛🏻‍♀️vampire-吸血鬼"},{level:2,title:"Cemetery(墓园)",slug:"cemetery-墓园"},{level:3,title:"Bury(埋葬)",slug:"bury-埋葬"},{level:3,title:"Revive(复活)",slug:"revive-复活"},{level:2,title:"Heaven(天国)",slug:"heaven-天国"},{level:2,title:"Priest (牧师)",slug:"priest-牧师"}]},{title:"错误处理",frontmatter:{sidebar:"auto",prev:"./hooks",next:"./unit-test"},regularPath:"/zh/guide/error.html",relativePath:"zh/guide/error.md",key:"v-59bfc02b",path:"/zh/guide/error.html",headers:[{level:2,title:"Gone中定义了三种错误类型:",slug:"gone中定义了三种错误类型"},{level:2,title:"为什么要定义这三种错误?",slug:"为什么要定义这三种错误"},{level:3,title:"服务器错误",slug:"服务器错误"},{level:3,title:"客户端错误",slug:"客户端错误"},{level:3,title:"业务错误",slug:"业务错误"},{level:2,title:"总结",slug:"总结"}]},{title:"Goner 和 依赖注入",frontmatter:{sidebar:"auto",prev:"./core-concept",next:"./goner-inject"},regularPath:"/zh/guide/gone-and-inject.html",relativePath:"zh/guide/gone-and-inject.md",key:"v-2e51fb11",path:"/zh/guide/gone-and-inject.html",headers:[{level:2,title:"Goner的定义",slug:"goner的定义"},{level:2,title:"依赖标记",slug:"依赖标记"},{level:2,title:"Goners 注册",slug:"goners-注册"},{level:2,title:"Priest函数",slug:"priest函数"}]},{title:"自动生成Priest",frontmatter:{sidebar:"auto",prev:"./schedule",next:"./hooks"},regularPath:"/zh/guide/auto-gen-priest.html",relativePath:"zh/guide/auto-gen-priest.md",key:"v-30329847",path:"/zh/guide/auto-gen-priest.html",headers:[{level:2,title:"小试牛刀",slug:"小试牛刀"},{level:3,title:"1. 安装gone辅助工具",slug:"_1-安装gone辅助工具"},{level:3,title:"2. 编写Goner",slug:"_2-编写goner"},{level:3,title:"3. 生成代码",slug:"_3-生成代码"},{level:2,title:"最佳实践",slug:"最佳实践"}]},{title:"如何优雅使用内置Goners?",frontmatter:{sidebar:"auto",prev:"./gone-vs-spring",next:"./config"},regularPath:"/zh/guide/inner-goner.html",relativePath:"zh/guide/inner-goner.md",key:"v-123e70c3",path:"/zh/guide/inner-goner.html",headers:[{level:2,title:"内置Goners",slug:"内置goners"},{level:2,title:"Priest函数",slug:"priest函数"},{level:2,title:"如何使用",slug:"如何使用"},{level:2,title:"内置Goner的GoneId",slug:"内置goner的goneid"}]},{title:"Gone支持哪些方式注入?",frontmatter:{sidebar:"auto",prev:"./gone-and-inject",next:"./gone-vs-spring"},regularPath:"/zh/guide/goner-inject.html",relativePath:"zh/guide/goner-inject.md",key:"v-e8628a72",path:"/zh/guide/goner-inject.html",headers:[{level:2,title:"公有属性注入 vs 私有属性注入",slug:"公有属性注入-vs-私有属性注入"},{level:2,title:"匿名注入 vs 具名注入",slug:"匿名注入-vs-具名注入"},{level:3,title:"匿名埋葬 和 具名埋葬",slug:"匿名埋葬-和-具名埋葬"},{level:3,title:"匿名注入",slug:"匿名注入"},{level:3,title:"具名注入",slug:"具名注入"},{level:2,title:"支持注入的属性类型",slug:"支持注入的属性类型"},{level:3,title:"1.指针类型",slug:"_1-指针类型"},{level:3,title:"2.值类型",slug:"_2-值类型"},{level:3,title:"3.接口类型",slug:"_3-接口类型"},{level:3,title:"4.Slice 类型",slug:"_4-slice-类型"},{level:3,title:"5.Map 类型",slug:"_5-map-类型"},{level:3,title:"指针注入 vs 接口注入",slug:"指针注入-vs-接口注入"},{level:3,title:"Slice注入 vs Map注入",slug:"slice注入-vs-map注入"}]},{title:"Gone的Hook函数",frontmatter:{sidebar:"auto",prev:"./auto-gen-priest",next:"./error"},regularPath:"/zh/guide/hooks.html",relativePath:"zh/guide/hooks.md",key:"v-7c7b70db",path:"/zh/guide/hooks.html",headers:[{level:2,title:"什么是Hook函数",slug:"什么是hook函数"},{level:2,title:"看一段代码",slug:"看一段代码"},{level:3,title:"Hook函数遵循以下规则",slug:"hook函数遵循以下规则"},{level:2,title:"Gone的启动流程",slug:"gone的启动流程"}]},{title:"利用redis提供分布式锁和分布式缓存",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./schedule"},regularPath:"/zh/guide/redis.html",relativePath:"zh/guide/redis.md",key:"v-6a7c97c5",path:"/zh/guide/redis.html",headers:[{level:2,title:"第一步:将redis相关Goner埋葬到Cemetery",slug:"第一步-将redis相关goner埋葬到cemetery"},{level:2,title:"第二步:在配置文件中增加redis相关配置",slug:"第二步-在配置文件中增加redis相关配置"},{level:2,title:"第三步,使用redis",slug:"第三步-使用redis"},{level:3,title:"注入接口",slug:"注入接口"},{level:3,title:"使用分布是缓存",slug:"使用分布是缓存"},{level:3,title:"使用分布时锁",slug:"使用分布时锁"},{level:2,title:"上面例子完整代码",slug:"上面例子完整代码"}]},{title:"对比 Gone 和 Spring 的依赖注入",frontmatter:{sidebar:"auto",sidebarDepth:4,prev:"./goner-inject",next:"./inner-goner"},regularPath:"/zh/guide/gone-vs-spring.html",relativePath:"zh/guide/gone-vs-spring.md",key:"v-d6a675f2",path:"/zh/guide/gone-vs-spring.html",headers:[{level:2,title:"依赖模块导入",slug:"依赖模块导入"},{level:3,title:"Spring",slug:"spring"},{level:3,title:"Gone",slug:"gone"},{level:2,title:"注入依赖",slug:"注入依赖"},{level:3,title:"Spring",slug:"spring-2"},{level:3,title:"Goner",slug:"goner"},{level:2,title:"配置注入",slug:"配置注入"},{level:3,title:"Spring",slug:"spring-3"},{level:2,title:"HTTP 参数注入",slug:"http-参数注入"},{level:3,title:"注入Query参数",slug:"注入query参数"},{level:3,title:"注入路径参数",slug:"注入路径参数"},{level:3,title:"请求体注入",slug:"请求体注入"},{level:2,title:"总结",slug:"总结"}]},{title:"用cron表达式配置定时任务",frontmatter:{sidebar:"auto",prev:"./redis",next:"./auto-gen-priest"},regularPath:"/zh/guide/schedule.html",relativePath:"zh/guide/schedule.md",key:"v-555fc7c7",path:"/zh/guide/schedule.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"编写定时任务执行的Job函数",slug:"编写定时任务执行的job函数"},{level:2,title:"设置定时任务",slug:"设置定时任务"},{level:2,title:"将定时配置放到配置文件中",slug:"将定时配置放到配置文件中"}]},{title:"单元测试",frontmatter:{sidebar:"auto",prev:"./error",next:!1},regularPath:"/zh/guide/unit-test.html",relativePath:"zh/guide/unit-test.md",key:"v-71628cc5",path:"/zh/guide/unit-test.html",headers:[{level:2,title:"假设我们编写的Goner如下",slug:"假设我们编写的goner如下"},{level:2,title:"我们可以编写测试文件如下:",slug:"我们可以编写测试文件如下"},{level:2,title:"使用gomock做mock测试",slug:"使用gomock做mock测试"}]},{title:"开发指南",frontmatter:{sidebar:"auto",prev:"../quick-start/",next:"../references/"},regularPath:"/zh/guide/",relativePath:"zh/guide/README.md",key:"v-46a3f904",path:"/zh/guide/",headers:[{level:2,title:"Gone的核心概念",slug:"gone的核心概念"},{level:2,title:"依赖注入",slug:"依赖注入"},{level:3,title:"goner和依赖注入",slug:"goner和依赖注入"},{level:3,title:"Gone支持哪些方式注入?",slug:"gone支持哪些方式注入"},{level:3,title:"对比 Gone 和 Spring 的依赖注入",slug:"对比-gone-和-spring-的依赖注入"},{level:2,title:"内置模块使用",slug:"内置模块使用"},{level:3,title:"如何优雅使用内置Goners?",slug:"如何优雅使用内置goners"},{level:3,title:"通过内置Goners支持配置文件",slug:"通过内置goners支持配置文件"},{level:3,title:"日志输出",slug:"日志输出"},{level:3,title:"使用traceId追踪日志",slug:"使用traceid追踪日志"},{level:3,title:"通过内置Goners支持数据库连接",slug:"通过内置goners支持数据库连接"},{level:3,title:"利用redis提供分布式锁和分布式缓存",slug:"利用redis提供分布式锁和分布式缓存"},{level:3,title:"用cron表达式配置定时任务",slug:"用cron表达式配置定时任务"},{level:2,title:"效率提升",slug:"效率提升"},{level:3,title:"自动生成Priest",slug:"自动生成priest"},{level:3,title:"Hook函数",slug:"hook函数"},{level:3,title:"错误处理",slug:"错误处理"},{level:3,title:"单元测试",slug:"单元测试"}]},{title:"日志输出",frontmatter:{sidebar:"auto",prev:"./config",next:"./tracer"},regularPath:"/zh/guide/logrus.html",relativePath:"zh/guide/logrus.md",key:"v-9162d5b2",path:"/zh/guide/logrus.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"在配置文件中添加相关配置",slug:"在配置文件中添加相关配置"},{level:2,title:"打印日志",slug:"打印日志"},{level:2,title:"日志的格式",slug:"日志的格式"},{level:2,title:"关于TraceId",slug:"关于traceid"}]},{title:"快速开始",frontmatter:{sidebar:"auto",prev:"../",next:"../guide/"},regularPath:"/zh/quick-start/",relativePath:"zh/quick-start/README.md",key:"v-3f115d44",path:"/zh/quick-start/",headers:[{level:2,title:"创建一个简单的Web项目",slug:"创建一个简单的web项目"},{level:2,title:"创建一个使用MySql做数据库,可以应用到生产环境的项目",slug:"创建一个使用mysql做数据库-可以应用到生产环境的项目"},{level:2,title:"更多例子",slug:"更多例子"}]},{title:"通过内置Goners支持数据库操作",frontmatter:{sidebar:"auto",prev:"./tracer",next:"./redis"},regularPath:"/zh/guide/xorm.html",relativePath:"zh/guide/xorm.md",key:"v-36b8ee32",path:"/zh/guide/xorm.html",headers:[{level:2,title:"以Mysql举个例子,源代码在这里可以找到",slug:"以mysql举个例子-源代码在这里可以找到"},{level:3,title:"1.准备mysql服务,可以使用docker-compose快速启动一个Mysql服务",slug:"_1-准备mysql服务-可以使用docker-compose快速启动一个mysql服务"},{level:3,title:"2. 创建mod和配置",slug:"_2-创建mod和配置"},{level:3,title:"3. 编写代码如下:",slug:"_3-编写代码如下"},{level:3,title:"3. 运行代码",slug:"_3-运行代码"},{level:2,title:"需要注意的点",slug:"需要注意的点"},{level:3,title:"1. github.com/gone-io/gone/goner 是对 xorm.io/xorm 的封装",slug:"_1-github-com-gone-io-gone-goner-是对-xorm-io-xorm-的封装"},{level:3,title:"2. 通过gone-xormGonerId注入",slug:"_2-通过gone-xormgonerid注入"},{level:3,title:"3. 注意导入的包",slug:"_3-注意导入的包"}]},{title:"使用traceId追踪日志",frontmatter:{sidebar:"auto",prev:"./logrus",next:"./xorm"},regularPath:"/zh/guide/tracer.html",relativePath:"zh/guide/tracer.md",key:"v-3a551ac7",path:"/zh/guide/tracer.html",headers:[{level:2,title:"将相关Goners埋葬到Cemetery",slug:"将相关goners埋葬到cemetery"},{level:2,title:"简单使用",slug:"简单使用"},{level:2,title:"跨协程(goroutine)传递traceId",slug:"跨协程-goroutine-传递traceid"},{level:2,title:"跨进程/服务传递traceId",slug:"跨进程-服务传递traceid"},{level:2,title:"多语言支持",slug:"多语言支持"}]},{title:"Web 项目",frontmatter:{sidebar:"auto",prev:!1,next:"./mysql"},regularPath:"/zh/quick-start/web.html",relativePath:"zh/quick-start/web.md",key:"v-7e64f407",path:"/zh/quick-start/web.html",headers:[{level:2,title:"安装gone辅助工具",slug:"安装gone辅助工具"},{level:2,title:"创建一个web项目并运行代码",slug:"创建一个web项目并运行代码"},{level:2,title:"项目结构",slug:"项目结构"},{level:2,title:"Router",slug:"router"},{level:2,title:"Controller",slug:"controller"},{level:2,title:"Service",slug:"service"},{level:2,title:"数据库操作",slug:"数据库操作"}]},{title:"Web + MySQL",frontmatter:{sidebar:"auto",prev:"./web",next:"./gpt"},regularPath:"/zh/quick-start/mysql.html",relativePath:"zh/quick-start/mysql.md",key:"v-e844302a",path:"/zh/quick-start/mysql.html",headers:[{level:2,title:"安装gone辅助工具",slug:"安装gone辅助工具"},{level:2,title:"使用gone命令创建项目",slug:"使用gone命令创建项目"},{level:2,title:"编译并运行",slug:"编译并运行"},{level:2,title:"测试接口",slug:"测试接口"},{level:2,title:"项目简单介绍",slug:"项目简单介绍"},{level:3,title:"目录结构",slug:"目录结构"},{level:3,title:"项目特点",slug:"项目特点"},{level:3,title:"部分代码讲解",slug:"部分代码讲解"},{level:3,title:"HTTP 注入",slug:"http-注入"},{level:3,title:"请求处理函数参数",slug:"请求处理函数参数"}]},{title:"封装一个Goner调用GPT",frontmatter:{sidebar:"auto",prev:"./mysql"},regularPath:"/zh/quick-start/gpt.html",relativePath:"zh/quick-start/gpt.md",key:"v-0f448959",path:"/zh/quick-start/gpt.html",headers:[{level:2,title:"定义用于依赖注入的接口",slug:"定义用于依赖注入的接口"},{level:2,title:"实现这个接口并嵌入gone.Flag",slug:"实现这个接口并嵌入gone-flag"},{level:2,title:"设计配置项",slug:"设计配置项"},{level:2,title:"使chatGPTClient实现接口AfterRevive() error函数,称为“先知”",slug:"使chatgptclient实现接口afterrevive-error函数-称为-先知"},{level:2,title:"方便用户使用",slug:"方便用户使用"},{level:2,title:"完整的疯转代码如下",slug:"完整的疯转代码如下"},{level:2,title:"编写测试",slug:"编写测试"},{level:2,title:"使用和编写示例",slug:"使用和编写示例"}]},{title:"JWT + MySQL + Redis + Schedule",frontmatter:{sidebar:"auto",prev:"./mysql",next:!1},regularPath:"/zh/quick-start/jwt+mysql+redis+schedule.html",relativePath:"zh/quick-start/jwt+mysql+redis+schedule.md",key:"v-bce8af32",path:"/zh/quick-start/jwt+mysql+redis+schedule.html",headers:[{level:2,title:"业务和接口说明",slug:"业务和接口说明"}]},{title:"一个极简例子",frontmatter:{sidebar:"auto",prev:!1,next:"./web"},regularPath:"/zh/quick-start/simple.html",relativePath:"zh/quick-start/simple.md",key:"v-4964c3a7",path:"/zh/quick-start/simple.html"},{title:"gone 辅助工具",frontmatter:{sidebar:"auto",prev:"./",next:"./http-inject"},regularPath:"/zh/references/gone-tool.html",relativePath:"zh/references/gone-tool.md",key:"v-347d4327",path:"/zh/references/gone-tool.html",headers:[{level:2,title:"安装",slug:"安装"},{level:2,title:"使用",slug:"使用"},{level:3,title:"测试是否正常安装",slug:"测试是否正常安装"},{level:3,title:"生成项目",slug:"生成项目"},{level:3,title:"生成Priest函数",slug:"生成priest函数"},{level:3,title:"生成Mock代码",slug:"生成mock代码"}]},{title:"Redis 参考文档",frontmatter:{sidebar:"auto",prev:"./xorm",next:"./"},regularPath:"/zh/references/redis.html",relativePath:"zh/references/redis.md",key:"v-735b4bc7",path:"/zh/references/redis.html",headers:[{level:2,title:"配置项",slug:"配置项"},{level:2,title:"import 和 bury",slug:"import-和-bury"},{level:2,title:"使用分布是缓存 redis.Cache",slug:"使用分布是缓存-redis-cache"},{level:3,title:"接口定义",slug:"接口定义"},{level:3,title:"使用示例",slug:"使用示例"},{level:2,title:"使用分布式锁 redis.Locker",slug:"使用分布式锁-redis-locker"},{level:3,title:"接口定义",slug:"接口定义-2"},{level:3,title:"使用示例",slug:"使用示例-2"},{level:2,title:"操作Key,使用 redis.Key",slug:"操作key-使用-redis-key"},{level:3,title:"接口定义",slug:"接口定义-3"},{level:2,title:"使用 Provider 注入 redis 接口",slug:"使用-provider-注入-redis-接口"},{level:3,title:"使用示例",slug:"使用示例-3"},{level:2,title:"直接使用redis连接池",slug:"直接使用redis连接池"},{level:3,title:"接口定义",slug:"接口定义-4"},{level:3,title:"使用示例",slug:"使用示例-4"}]},{title:"HTTP 注入说明",frontmatter:{sidebar:"auto",prev:"./gone-tool",next:"./xorm"},regularPath:"/zh/references/http-inject.html",relativePath:"zh/references/http-inject.md",key:"v-3c133eb2",path:"/zh/references/http-inject.html",headers:[{level:2,title:"HTTP 依赖注入标签的格式",slug:"http-依赖注入标签的格式"},{level:2,title:"支持注入的类型和响应标签",slug:"支持注入的类型和响应标签"},{level:2,title:"Query参数注入",slug:"query参数注入"},{level:3,title:"属性类型为简单类型",slug:"属性类型为简单类型-1"},{level:3,title:"属性类型为简单类型的数组",slug:"属性类型为简单类型的数组"},{level:3,title:"属性类型为结构体或者结构体指针",slug:"属性类型为结构体或者结构体指针"},{level:2,title:"URL路径参数注入",slug:"url路径参数注入"},{level:2,title:"Body注入",slug:"body注入"},{level:2,title:"请求头注入",slug:"请求头注入"},{level:2,title:"Cookie注入",slug:"cookie注入"},{level:2,title:"高级",slug:"高级"},{level:3,title:"URL结构体注入",slug:"url结构体注入"},{level:3,title:"请求头注入",slug:"请求头注入-2"},{level:3,title:"上下文结构体注入",slug:"上下文结构体注入"},{level:3,title:"请求结构体注入",slug:"请求结构体注入"},{level:3,title:"请求响应接口注入",slug:"请求响应接口注入"},{level:2,title:"备注",slug:"备注"}]},{title:"Gone对Xorm的增强",frontmatter:{sidebar:"auto",prev:"./http-inject",next:"./redis"},regularPath:"/zh/references/xorm.html",relativePath:"zh/references/xorm.md",key:"v-40feb13a",path:"/zh/references/xorm.html",headers:[{level:2,title:"1. 自动事务",slug:"_1-自动事务"},{level:2,title:"2. 事务自动传递",slug:"_2-事务自动传递"},{level:2,title:"3. SQL支持名字参数",slug:"_3-sql支持名字参数"}]},{title:"参考文档",frontmatter:{sidebar:"auto",prev:"../guide/",next:"../goners/"},regularPath:"/zh/references/",relativePath:"zh/references/README.md",key:"v-7f6e5700",path:"/zh/references/",headers:[{level:2,title:"gone 辅助工具",slug:"gone-辅助工具"},{level:2,title:"HTTP 注入说明",slug:"http-注入说明"},{level:2,title:"Gone对Xorm的增强",slug:"gone对xorm的增强"},{level:2,title:"Redis 参考文档",slug:"redis-参考文档"}]}],themeConfig:{repo:"gone-io/gone",docsDir:"docs",logo:"/assets/img/logo.png",locales:{"/":{label:"English",selectText:"Languages",editLinkText:"Edit this page on GitHub",nav:[{text:"Introduction",link:"/"},{text:"Quick Start",link:"/quick-start/",items:[{text:"Creating a Simple Web Project",link:"/quick-start/simple.md"},{text:"Web + MySQL",link:"/quick-start/mysql.md"}]},{text:"Development Guide",link:"/guide/",items:[{text:"Core Concepts of Gone",link:"/guide/core-concept.md"},{text:"Goner and Dependency Injection",link:"/guide/gone-and-inject.md"},{text:"Injection Methods Supported by Gone",link:"/guide/goner-inject.md"},{text:"How to Gracefully Use Built-in Goners?",link:"/guide/inner-goner.md"},{text:"Configuring with Built-in Goners Support",link:"/guide/config.md"},{text:"Performing database operations",link:"/guide/xorm.md"},{text:"Gone's Hook Functions",link:"/guide/hooks.md"},{text:"Logging Output",link:"/guide/logrus.md"},{text:"Trace Log with traceId",link:"/guide/tracer.md"},{text:"Auto-generate Priest",link:"/guide/auto-gen-priest.md"},{text:"Using Redis for Distributed Locking and Caching",link:"/guide/redis"},{text:"Unit Testing",link:"/guide/unit-test.md"}]},{text:"References",link:"/references/"},{text:"Goners",link:"/goners/"}],sidebar:[["/","介绍"],"/quick-start/","/guide/","/guide/core-concept"]},"/zh/":{label:"简体中文",selectText:"选择语言",editLinkText:"在 GitHub 上编辑此页",nav:[{text:"介绍",link:"/zh/"},{text:"快速开始",link:"/zh/quick-start/",items:[{text:"极简例子",link:"/zh/quick-start/simple.md"},{text:"Web项目",link:"/zh/quick-start/web.md"},{text:"Web + MySQL",link:"/zh/quick-start/mysql.md"},{text:"封装一个Goner调用GPT",link:"/zh/quick-start/gpt.md"}]},{text:"开发指南",link:"/zh/guide/",items:[{text:"核心概念",link:"/zh/guide/core-concept.md"},{text:"依赖注入",items:[{text:"Goner和依赖注入",link:"/zh/guide/gone-and-inject.md"},{text:"支持哪些方式注入?",link:"/zh/guide/goner-inject.md"},{text:"Gone vs Spring",link:"/zh/guide/gone-vs-spring.md"}]},{text:"内置模块使用",items:[{text:"内置Goners",link:"/zh/guide/inner-goner.md"},{text:"配置读取",link:"/zh/guide/config.md"},{text:"日志输出",link:"/zh/guide/logrus.md"},{text:"链路追踪",link:"/zh/guide/tracer.md"},{text:"操作数据库",link:"/zh/guide/xorm.md"},{text:"分布式锁 + 分布式缓存",link:"/zh/guide/redis"},{text:"cron 格式的任务",link:"/zh/guide/schedule.md"}]},{text:"效率提升",items:[{text:"自动生成 Priest",link:"/zh/guide/auto-gen-priest.md"},{text:"Hook 函数",link:"/zh/guide/hooks.md"},{text:"错误处理",link:"/zh/guide/error.md"},{text:"单元测试",link:"/zh/guide/unit-test.md"}]}]},{text:"参考文档",link:"/zh/references/"},{text:"组件库",link:"/zh/goners/"},{text:"博客",link:"/zh/blogs/"}],sidebar:["/zh/","/zh/quick-start/","/zh/guide/","/zh/guide/core-concept"]}}},locales:{"/zh/":{lang:"zh-CN",title:"Gone 文档",description:"Gone是一个轻量的,基于Golang的,依赖注入框架,集成了Gin、XOrm、Logrus等众多开源工具,用于提供便捷的Web开发。",path:"/zh/"},"/":{lang:"en-US",title:"Gone Document",description:"Gone is a lightweight dependency injection framework based on Golang, integrating many open-source tools such as Gin, XOrm, Logrus, etc., to provide convenient web development.",path:"/"}}};n(236);Wn.component("Badge",()=>Promise.all([n.e(0),n.e(4)]).then(n.bind(null,312))),Wn.component("CodeGroup",()=>Promise.all([n.e(0),n.e(6)]).then(n.bind(null,309))),Wn.component("CodeBlock",()=>Promise.all([n.e(0),n.e(5)]).then(n.bind(null,308)));n(237);var As={props:{color:{required:!1,default:"rgb(66, 185, 131)"}}},zs=(n(238),Object(Os.a)(As,(function(){return(0,this._self._c)("div",{staticClass:"spinner",style:{background:this.color}})}),[],!1,null,"1bbcb91a",null).exports);const Ls={name:"Mermaid",props:{id:{type:String,required:!1,default:()=>"diagram_"+Date.now()},graph:{type:String,required:!1}},data:()=>({svg:void 0}),computed:{graphData(){return this.graph?this.graph:this.$slots.default[0].text}},render(e){return void 0===this.svg?e("Loading"):e("div",{class:["mermaid-diagram"],domProps:{innerHTML:this.svg,style:"width: 100%"}})},mounted(){n.e(74).then(n.t.bind(null,304,7)).then(e=>{e.initialize({startOnLoad:!0}),e.render(this.id,this.graphData,e=>{this.svg=e})})},components:{Loading:zs}};var Is=n(45);const Rs={locales:!0};var Ms=[{},({Vue:e})=>{e.mixin({computed:{$dataBlock(){return this.$options.__data__block__}}})},{},{},({Vue:e})=>{e.component(Ls.name,Ls)},({router:e,siteData:t})=>{const{routes:n=[]}=e.options,{redirectors:r=[]}=Rs;function o(e){return n.some(t=>t.path.toLowerCase()===e.toLowerCase())}function i(e){if(o(e))return e;if(!/\/$/.test(e)){const t=e+"/";if(o(t))return t}if(!/\.html$/.test(e)){const t=e.replace(/\/$/,"")+".html";if(o(t))return t}return null}if(Rs.locales&&t.locales){const e=t.locales,n=Object.keys(e),o=n.map(t=>({key:t.replace(/^\/|\/$/,""),lang:e[t].lang}));"object"!=typeof Rs.locales&&(Rs.locales={});const{fallback:i,storage:a=!0}=Rs.locales;i&&n.unshift(i),r.unshift({storage:a,base:"/",alternative(){if("undefined"!=typeof window&&window.navigator){const e=window.navigator.languages||[window.navigator.language],t=o.find(({lang:t})=>e.includes(t));if(t)return t.key}return n}})}const a=r.map(({base:e="/",storage:t=!1,alternative:n})=>{let r=!1;if(t)if("object"!=typeof t){const n="string"!=typeof t?"vuepress:redirect:"+e:t;r={get:()=>"undefined"==typeof localStorage?null:localStorage.getItem(n),set(e){"undefined"!=typeof localStorage&&localStorage.setItem(n,e)}}}else t.get&&t.set&&(r=t);return{base:e,storage:r,alternative:n}});e.beforeEach((e,t,n)=>{if(i(e.path))return n();let r;for(const t of a){const{base:n="/",storage:o=!1}=t;let{alternative:a}=t;if(!e.path.startsWith(n))continue;const s=e.path.slice(n.length)||"/";if(o){const e=o.get(t);if(e){const t=i(Object(Is.join)(n,e,s));if(t){r=t;break}}}if("function"==typeof a&&(a=a(s)),a){"string"==typeof a&&(a=[a]);for(const e of a){const t=i(Object(Is.join)(n,e,s));if(t){r=t;break}}if(r)break}}n(r)}),e.afterEach(e=>{if(o(e.path))for(const t of a){const{base:n,storage:r}=t;if(!r||!e.path.startsWith(n))continue;const o=e.path.slice(n.length).split("/")[0];o&&r.set(o,t)}})}],Ds=[];class qs extends class{constructor(){this.store=new Wn({data:{state:{}}})}$get(e){return this.store.state[e]}$set(e,t){Wn.set(this.store.state,e,t)}$emit(...e){this.store.$emit(...e)}$on(...e){this.store.$on(...e)}}{}Object.assign(qs.prototype,{getPageAsyncComponent:ss,getLayoutAsyncComponent:ls,getAsyncComponent:us,getVueComponent:cs});var Gs={install(e){const t=new qs;e.$vuepress=t,e.prototype.$vuepress=t}};function Ns(e,t){const n=t.toLowerCase();return e.options.routes.some(e=>e.path.toLowerCase()===n)}var Fs={props:{pageKey:String,slotKey:{type:String,default:"default"}},render(e){const t=this.pageKey||this.$parent.$page.key;return ps("pageKey",t),Wn.component(t)||Wn.component(t,ss(t)),Wn.component(t)?e(t):e("")}},Us={functional:!0,props:{slotKey:String,required:!0},render:(e,{props:t,slots:n})=>e("div",{class:["content__"+t.slotKey]},n()[t.slotKey])},Bs={computed:{openInNewWindowTitle(){return this.$themeLocaleConfig.openNewWindowText||"(opens new window)"}}},Hs=(n(239),n(240),Object(Os.a)(Bs,(function(){var e=this._self._c;return e("span",[e("svg",{staticClass:"icon outbound",attrs:{xmlns:"http://www.w3.org/2000/svg","aria-hidden":"true",focusable:"false",x:"0px",y:"0px",viewBox:"0 0 100 100",width:"15",height:"15"}},[e("path",{attrs:{fill:"currentColor",d:"M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"}}),this._v(" "),e("polygon",{attrs:{fill:"currentColor",points:"45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"}})]),this._v(" "),e("span",{staticClass:"sr-only"},[this._v(this._s(this.openInNewWindowTitle))])])}),[],!1,null,null,null).exports),Vs={functional:!0,render(e,{parent:t,children:n}){if(t._isMounted)return n;t.$once("hook:mounted",()=>{t.$forceUpdate()})}};Wn.config.productionTip=!1,Wn.use(Ha),Wn.use(Gs),Wn.mixin(function(e,t,n=Wn){!function(e){e.locales&&Object.keys(e.locales).forEach(t=>{e.locales[t].path=t});Object.freeze(e)}(t),n.$vuepress.$set("siteData",t);const r=new(e(n.$vuepress.$get("siteData"))),o=Object.getOwnPropertyDescriptors(Object.getPrototypeOf(r)),i={};return Object.keys(o).reduce((e,t)=>(t.startsWith("$")&&(e[t]=o[t].get),e),i),{computed:i}}(e=>class{setPage(e){this.__page=e}get $site(){return e}get $themeConfig(){return this.$site.themeConfig}get $frontmatter(){return this.$page.frontmatter}get $localeConfig(){const{locales:e={}}=this.$site;let t,n;for(const r in e)"/"===r?n=e[r]:0===this.$page.path.indexOf(r)&&(t=e[r]);return t||n||{}}get $siteTitle(){return this.$localeConfig.title||this.$site.title||""}get $canonicalUrl(){const{canonicalUrl:e}=this.$page.frontmatter;return"string"==typeof e&&e}get $title(){const e=this.$page,{metaTitle:t}=this.$page.frontmatter;if("string"==typeof t)return t;const n=this.$siteTitle,r=e.frontmatter.home?null:e.frontmatter.title||e.title;return n?r?r+" | "+n:n:r||"VuePress"}get $description(){const e=function(e){if(e){const t=e.filter(e=>"description"===e.name)[0];if(t)return t.content}}(this.$page.frontmatter.meta);return e||(this.$page.frontmatter.description||this.$localeConfig.description||this.$site.description||"")}get $lang(){return this.$page.frontmatter.lang||this.$localeConfig.lang||"en-US"}get $localePath(){return this.$localeConfig.path||"/"}get $themeLocaleConfig(){return(this.$site.themeConfig.locales||{})[this.$localePath]||{}}get $page(){return this.__page?this.__page:function(e,t){for(let n=0;nn||(e.hash?!Wn.$vuepress.$get("disableScrollBehavior")&&{selector:decodeURIComponent(e.hash)}:{x:0,y:0})});!function(e){e.beforeEach((t,n,r)=>{if(Ns(e,t.path))r();else if(/(\/|\.html)$/.test(t.path))if(/\/$/.test(t.path)){const n=t.path.replace(/\/$/,"")+".html";Ns(e,n)?r(n):r()}else r();else{const n=t.path+"/",o=t.path+".html";Ns(e,o)?r(o):Ns(e,n)?r(n):r()}})}(n);const r={};try{await Promise.all(Ms.filter(e=>"function"==typeof e).map(t=>t({Vue:Wn,options:r,router:n,siteData:Ts,isServer:e})))}catch(e){console.error(e)}return{app:new Wn(Object.assign(r,{router:n,render:e=>e("div",{attrs:{id:"app"}},[e("RouterView",{ref:"layout"}),e("div",{class:"global-ui"},Ds.map(t=>e(t)))])})),router:n}}(!1).then(({app:e,router:t})=>{t.onReady(()=>{e.$mount("#app")})})}]); \ No newline at end of file diff --git a/goners/index.html b/goners/index.html index 3260b01..f724a6a 100644 --- a/goners/index.html +++ b/goners/index.html @@ -21,7 +21,7 @@ } - + @@ -114,6 +114,6 @@ →

- + diff --git a/guide/auto-gen-priest.html b/guide/auto-gen-priest.html index 55b7eb3..9e3aea7 100644 --- a/guide/auto-gen-priest.html +++ b/guide/auto-gen-priest.html @@ -21,7 +21,7 @@ } - + @@ -151,6 +151,6 @@ →

- + diff --git a/guide/config.html b/guide/config.html index cf10faf..0a915ef 100644 --- a/guide/config.html +++ b/guide/config.html @@ -21,7 +21,7 @@ } - + @@ -200,6 +200,6 @@ →

- + diff --git a/guide/core-concept.html b/guide/core-concept.html index 5d2aaa4..5388709 100644 --- a/guide/core-concept.html +++ b/guide/core-concept.html @@ -21,7 +21,7 @@ } - + @@ -202,6 +202,6 @@ →

- + diff --git a/guide/gone-and-inject.html b/guide/gone-and-inject.html index eef95d5..3679748 100644 --- a/guide/gone-and-inject.html +++ b/guide/gone-and-inject.html @@ -21,7 +21,7 @@ } - + @@ -375,6 +375,6 @@ →

- + diff --git a/guide/goner-inject.html b/guide/goner-inject.html index 7b64127..e292199 100644 --- a/guide/goner-inject.html +++ b/guide/goner-inject.html @@ -21,7 +21,7 @@ } - + @@ -295,6 +295,6 @@ →

- + diff --git a/guide/hooks.html b/guide/hooks.html index dd10fcf..a784b7e 100644 --- a/guide/hooks.html +++ b/guide/hooks.html @@ -21,7 +21,7 @@ } - + @@ -154,6 +154,6 @@ →

- + diff --git a/guide/index.html b/guide/index.html index 009d582..701481a 100644 --- a/guide/index.html +++ b/guide/index.html @@ -21,7 +21,7 @@ } - + @@ -114,6 +114,6 @@ →

- + diff --git a/guide/inner-goner.html b/guide/inner-goner.html index ded9612..5f07c15 100644 --- a/guide/inner-goner.html +++ b/guide/inner-goner.html @@ -21,7 +21,7 @@ } - + @@ -219,6 +219,6 @@ →

- + diff --git a/guide/logrus.html b/guide/logrus.html index 022b280..fe9bbeb 100644 --- a/guide/logrus.html +++ b/guide/logrus.html @@ -21,7 +21,7 @@ } - + @@ -164,6 +164,6 @@ →

- + diff --git a/guide/redis.html b/guide/redis.html index b35de04..b2ea2e0 100644 --- a/guide/redis.html +++ b/guide/redis.html @@ -21,7 +21,7 @@ } - + @@ -261,6 +261,6 @@ →

- + diff --git a/guide/tracer.html b/guide/tracer.html index 46828c0..2a6e4e7 100644 --- a/guide/tracer.html +++ b/guide/tracer.html @@ -21,7 +21,7 @@ } - + @@ -148,6 +148,6 @@ →

- + diff --git a/guide/unit-test.html b/guide/unit-test.html index 2d066a8..741f54e 100644 --- a/guide/unit-test.html +++ b/guide/unit-test.html @@ -21,7 +21,7 @@ } - + @@ -245,6 +245,6 @@

- + diff --git a/guide/xorm.html b/guide/xorm.html index c21c549..44c278c 100644 --- a/guide/xorm.html +++ b/guide/xorm.html @@ -21,7 +21,7 @@ } - + @@ -235,6 +235,6 @@ →

- + diff --git a/index.html b/index.html index bd89cab..4cd08e2 100644 --- a/index.html +++ b/index.html @@ -21,7 +21,7 @@ } - + @@ -152,6 +152,6 @@ →

- + diff --git a/quick-start/index.html b/quick-start/index.html index 8cbdc22..7ad759d 100644 --- a/quick-start/index.html +++ b/quick-start/index.html @@ -21,7 +21,7 @@ } - + @@ -114,6 +114,6 @@ →

- + diff --git a/quick-start/mysql.html b/quick-start/mysql.html index 3b0ff23..7f1a19d 100644 --- a/quick-start/mysql.html +++ b/quick-start/mysql.html @@ -21,7 +21,7 @@ } - + @@ -265,6 +265,6 @@

- + diff --git a/quick-start/simple.html b/quick-start/simple.html index b5aba52..cfd8c1d 100644 --- a/quick-start/simple.html +++ b/quick-start/simple.html @@ -21,7 +21,7 @@ } - + @@ -247,6 +247,6 @@ →

- + diff --git a/references/gone-tool.html b/references/gone-tool.html index ba7af24..3898c6a 100644 --- a/references/gone-tool.html +++ b/references/gone-tool.html @@ -21,7 +21,7 @@ } - + @@ -125,6 +125,6 @@ →

- + diff --git a/references/http-inject.html b/references/http-inject.html index c01666f..20a80c5 100644 --- a/references/http-inject.html +++ b/references/http-inject.html @@ -21,7 +21,7 @@ } - + @@ -124,6 +124,6 @@

- + diff --git a/references/index.html b/references/index.html index f82fc11..b1935ab 100644 --- a/references/index.html +++ b/references/index.html @@ -21,7 +21,7 @@ } - + @@ -114,6 +114,6 @@ →

- + diff --git a/zh/blogs/1_v1.x_release.html b/zh/blogs/1_v1.x_release.html index 90ebaec..186af69 100644 --- a/zh/blogs/1_v1.x_release.html +++ b/zh/blogs/1_v1.x_release.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/blogs/index.html b/zh/blogs/index.html index 41e3f6a..29dc42a 100644 --- a/zh/blogs/index.html +++ b/zh/blogs/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/goners/index.html b/zh/goners/index.html index 3fd08b3..d093ac1 100644 --- a/zh/goners/index.html +++ b/zh/goners/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/auto-gen-priest.html b/zh/guide/auto-gen-priest.html index 7d35042..05c91c9 100644 --- a/zh/guide/auto-gen-priest.html +++ b/zh/guide/auto-gen-priest.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/config.html b/zh/guide/config.html index 41567fa..834aea4 100644 --- a/zh/guide/config.html +++ b/zh/guide/config.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/core-concept.html b/zh/guide/core-concept.html index 0c774b4..893fc4d 100644 --- a/zh/guide/core-concept.html +++ b/zh/guide/core-concept.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/error.html b/zh/guide/error.html index 5dd8024..e84f4df 100644 --- a/zh/guide/error.html +++ b/zh/guide/error.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/gone-and-inject.html b/zh/guide/gone-and-inject.html index f234a6c..0087699 100644 --- a/zh/guide/gone-and-inject.html +++ b/zh/guide/gone-and-inject.html @@ -3,7 +3,7 @@ - Goner和依赖注入 | Gone 文档 + Goner 和 依赖注入 | Gone 文档 - + @@ -72,9 +72,9 @@ GitHub - (opens new window)

# Goner和依赖注入

# Goner的定义

在Gone应用中,所有的组件都被要求定义为Goner(就是“继承”了gone.Flag的结构体,实际上golang中根本没有“集成”这个概念,它有的只有匿名嵌套);如果Goner的某个属性标注了gone:""标签,Gone框架将尝试自动装配该属性。下面是定义一个Goner的例子:

package example
-import "github.com/gone-io/gone"
-type AGoner struct {
-	gone.Flag
-}
-

在另一个Goner中注入上面定义的AGoner:

package example
-import "github.com/gone-io/gone"
-type BGoner struct{
-    gone.Flag
-    A *AGoner `gone:"*"` //gone标签的作用在于告诉Gone,该属性需要被自动注入一个值
+     (opens new window)    

# Goner 和 依赖注入

Gone 作为一个依赖注入框架,首先需要回答的问题:什么是依赖,如何定义依赖?

在golang中已经定义了package,我们在项目中引入的package就是一种依赖;这些依赖可以利用golang项目中的.mod文件进行比较好的管理,然而这种依赖也并不是我们讨论的需要注入的依赖。

“依赖注入”,作为一个动补短语,要明确它的含义,需要问:什么依赖了什么,谁注入了谁?

在golang中,能够承载业务逻辑的结构,只有 funcstructfunc 的执行结果 依赖 函数的参数,而 struct 功能的实现 依赖 结构体的属性。无论是函数的参数 还是 结构体的属性,都是业务逻辑实现的 依赖;这样我们就回答了“什么是依赖?”:

定义

依赖,是为了实现某业务逻辑,函数或者结构体需要依赖的 外部值 或者 外部参数。

这些外部值或者外部参数,可能是代表了业务逻辑依赖的外部业务;实现我们的业务所需的业务逻辑,需要依赖这些外部业务来完成。由此可见,是 业务 依赖了 业务,那么注入的也是业务,是 业务 注入了 业务

为了对业务进行抽象,我们定义了Goner,它是一个接口,所有业务结构体都要实现它;那么,在Gone框架中,就是 Goners依赖Goners,Goners注入到Goners

# Goner的定义

我们查看Goner (opens new window)的定义,如下:

type Goner interface {
+	goneFlag()
 }
-

其中,注入的和被注入的结构体都要求是Goner(也就是匿名嵌套了gone.Flag的结构体),BGonerA属性的gone:"*"标签的作用在于告诉框架:这个属性需要被注入一个值。

# 在Gone中是如何完成依赖注入的?

在Java Spring中,给class打上@Component@Service等标注,Spring启动时会自动扫描到这些特殊的类,然后实例化他们并且给他们有特定标注的属性注入对应的值。

Spring之所以能够实现这样的功能,Java有一个特性很关键,就是Java代码在编译成jar后,会保留所有class的字节码,哪怕是没有被main函数依赖的class代码;然而,在Golang中,编译后的代码会被裁剪,二进制文件中只会保留main函数依赖的相关代码。所以我们仅是定义Goner,在编译后我们会发现我们Goner代码全部被裁剪了。

如何让我们的Goners不被裁剪掉呢?答案很简单,我们显式的将所有Goner加入到一个”仓库“中;在Gone中,这个仓库叫做CemeteryGoner有“死者”的意思;Cemetery是墓地,用于埋葬(Bury)Goner。我们可以在程序启动时,将所有的Goner实例化后并加入到Cemetery中:

package main
-import "example"
+

可以看到,Goner是一个接口,要求实现一个私有方法goneFlag(),这个方法是为了标识该结构体是Goner。由于golang语法限制,在包外部是无法实现一个包内的私有方法的,为了在外部的业务代码可以实现 Goner接口 ,我们又定义了gone.Flag结构体,并且在实现了goneFlag()方法时将方法的接收者设置了为 Flag指针,如下:

type Flag struct{}
+func (g *Flag) goneFlag() {}
+

这样,业务结构体XBusiness 有且仅有 匿名嵌入gone.Flag才能完成对Goner接口的实现。gone框架要求只有Goner才可以被注册到Gone框架中,用于依赖注入的装配。 +例如:

package example
 import "github.com/gone-io/gone"
-func main() {
-	gone.Run(func(cemetery gone.Cemetery) error {
-        cemetery.Bury(&example.AGoner{})
-		cemetery.Bury(&example.BGoner{})
-        return nil
-	})
-}
-

在上面的代码中,我们看到gone.Run可以接收形式如 func (cemetery gone.Cemetery) error 的函数;实际上这个函数,我们称之为 Priest,是牧师的意思,他专门负责将 Goner 埋葬到 墓地(Cemetery)。

# 如何执行Goner中的业务代码?

在Gone中,我们做了一个有趣的定义:Goner所有属性都被注入后,我们称这个Goner被复活了(Revive)。如果你查看Gone的源代码,你会发现管理复活Goner状态的组件叫Heaven天堂),这里灵感来源于各宗教神话中人死后会上天堂的传说。

为了执行Goner业务代码,我们可以在Goner上定义了方法 AfterRevive() gone.AfterReviveError,这个方法会在GonerRevive 后得到执行,并且我们将拥有该方法的 Goner 称之为 Prophet(也就是 先知)。实际上,一般情况我们只需要定义少量的先知来引导代码执行就可以了。

下面是一个例子;代码可以在这里 (opens new window)找到:

package main
-import "github.com/gone-io/gone"
-type Adder struct {
+type XBusiness struct {
 	gone.Flag
 }
-func (a *Adder) Add(a1, a2 int) int {
-	return a1 + a2
-}
-type Computer struct {
-	gone.Flag
-	adder Adder `gone:"*"`
-}
-func (c *Computer) Compute() {
-	println("I want to compute!")
-	println("1000 add 2000 is", c.adder.Add(1000, 2000))
-}
-// AfterRevive 复活后执行的函数
-func (c *Computer) AfterRevive() gone.AfterReviveError {
-	// boot
-	c.Compute()
-	return nil
+

这样设计的好处是:

  1. 限定依赖注入装配的对象都是Goner,可以简化了依赖注入装配流程实现。
  2. 限定Goner一定是指针,依赖注入时可以避免结构体的值拷贝;一方面是提高性能,另一方面是避免值拷贝时的“浅拷贝”业务对象带来的未知问题。

# 依赖标记

业务结构体中,并不是所有的属性都需要依赖注入,需要有一种机制标记哪些属性需要使用依赖注入。为此,我们设计了标签gone来标记需要注入的属性,如下:

type Employee interface {
+	Work()
 }
-func main() {
-	gone.Run(func(cemetery gone.Cemetery) error {
-		cemetery.Bury(&Computer{})
-		cemetery.Bury(&Adder{})
-		return nil
-	})
-}
-

执行上面代码,将得到结果:

Revive github.com/gone-io/gone/heaven
-Revive github.com/gone-io/gone/cemetery
-Revive main/Computer
-Revive main/Adder
-I want to compute!
-1000 add 2000 is 3000
-

# gone命令,自动生成Priest函数

实际上,前面已经讲完了Gone框架的核心功能;然而由于Golang本身的问题,我们无法做到像Spring那么方便,需要手动把所有 Goner 加入(Bury)到 Cemetery。为了让Gone使用起来更方便,我们编写了一个辅助工具来自动生成 Priest 函数。下面介绍如何在一个项目中使用这个辅助工具。

完整代码可以在这里 (opens new window)找到

# 1. 安装辅助工具: gone

辅助工具和Gone框架同名,也叫gone,可以使用go install进行安装,如下:

go install github.com/gone-io/gone/tools/gone@latest
-

gone 命令的使用可以参考:gone辅助工具 (opens new window)

# 2. 创建一个名为gen-code的新项目

mkdir gen-code
-cd gen-code
-go mod init gen-code
-

# 3. 创建Goner

文件名:goner.go

package main
-import "github.com/gone-io/gone"
-//go:gone
-func NewAdder() gone.Goner {
-	return &Adder{}
-}
-//go:gone
-func NewComputer() gone.Goner {
-	return &Computer{}
-}
-type Adder struct {
+type Company struct {
 	gone.Flag
+	Boss Employee `gone:"*"` // 标记需要依赖注入的属性
 }
-func (a *Adder) Add(a1, a2 int) int {
-	return a1 + a2
-}
-type Computer struct {
+

公司需要注入一个员工作为Boss,星号(*)表示该属性的注入只需要匹配类型,即实现 Employee 接口的Goner就可以;这种注入方式,我们称为匿名注入

需要注意

Goner 结构体需要注入的属性,可以是任意 接口 或者 结构体指针,这里并不限定是Goner,只有Goner注册到Gone框架时限定Goner类型。

公司的老板不可能是任何员工都能担任的,与匿名注入对应的就是具名注入,注入标签可以将星号替换为需要注入Goner的Id 字符串,如下:

type Company struct {
 	gone.Flag
-	adder Adder `gone:"*"`
+	Boss Employee `gone:"boss"` // 具名注入,要求该属性注入一个Id=boss的Goner
 }
-func (c *Computer) Compute() {
-	println("I want to compute!")
-	println("1000 add 2000 is", c.adder.Add(1000, 2000))
-}
-// AfterRevive 复活后执行的函数
-func (c *Computer) AfterRevive() gone.AfterReviveError {
-	// boot
-	c.Compute()
-	return nil
-}
-

在上面代码中,请注意我们添加了两个工厂函数 NewAdder() gone.Gonerfunc NewComputer() gone.Goner,并且在函数前做了一个特殊的注释:

//go:gone
-

请不要删除这个注释,这个注释的作用是告诉辅助工具如何生成代码的。

# 4. 使用辅助工具

请在gen-code目录下执行下面命令:

gone priest -s . -p main -f Priest -o priest.go
-

这个命令的含义是,扫描当前目录生成一个 牧师 函数,它的函数名为 Priest,所在的包名为main,代码放到名为priest.go的文件中。 -代码执行完后,会在当前目录中生成一个文件priest.go,它的内容如下:

// Code generated by gone; DO NOT EDIT.
-package main
-import (
-    "github.com/gone-io/gone"
-)
-func Priest(cemetery gone.Cemetery) error {
-    cemetery.Bury(NewAdder())
-    cemetery.Bury(NewComputer())
-	return nil
-}
-

# 5. 添加main函数

文件名:main.go

package main
-import "github.com/gone-io/gone"
-func main() {
-	gone.Run(Priest)
-}
-

至此,我们就完成了整个小的Gone程序,它的文件结构如下:

.
-├── go.mod
-├── go.sum
-├── goner.go   #  goner的定义
-├── main.go
-└── priest.go  # 生成的代码
-

可以通过命令go run .来执行,程序将输出如下内容:

Revive github.com/gone-io/gone/heaven
-Revive github.com/gone-io/gone/cemetery
-Revive main/Adder
-Revive main/Computer
-I want to compute!
-1000 add 2000 is 3000
-

# 匿名注入与具名注入

在前面的篇章中,我们所有的例子实际上都是按类型的匿名注入;匿名注入时,如果存在多个同类型的Goner,被注入只会是其中一个,通常是最先被复活的那个。在Gone中,是支持按名字注入的(也就是具名注入)。

首先,Cemetery.Bury 函数的完整定义是这样的 Bury(Goner, ...GonerId) Cemetery,这样的定义有两层考虑:

    1. 用于支持具名埋葬,第二个参数是可选的,允许传入一个字符串作为GonerIDGonerId);
    1. 使Bury函数支持链式调用。

# 具名埋葬

实现具名埋葬,我们的代码可以这样写:

// Priest Responsible for putting Goners that need to be used into the framework
-func Priest(cemetery gone.Cemetery) error {
-	cemetery.
-		Bury(&AGoner{Name: "Injected Goner1"}, "A1"). //埋葬第一个AGoner,ID=A1
-		Bury(&AGoner{Name: "Injected Goner2"}, "A2"). //埋葬第二个AGoner,ID=A2
-		Bury(&BGoner{})
-	return nil
-}
-

另外也可以这样写,这样写的好处是将Goner的构造和埋葬进行解耦:

// NewA1 构造A1 AGoner
-func NewA1() (gone.Goner, gone.GonerId) {
-	return &AGoner{Name: "Injected Goner1"}, "A1"
-}
-// NewA2 构造A2 AGoner
-func NewA2() (gone.Goner, gone.GonerId) {
-	return &AGoner{Name: "Injected Goner2"}, "A2"
-}
-// Priest Responsible for putting Goners that need to be used into the framework
-func Priest(cemetery gone.Cemetery) error {
-	cemetery.
-		Bury(NewA1()).
-		Bury(NewA2()).
-		Bury(&BGoner{})
-	return nil
-}
-

然后,就是结构体的具名注入,举个例子就能立刻明白:

type BGoner struct {
-	gone.Flag         //tell the framework that this struct is a Goner
-	a         *AGoner `gone:"*"` //匿名注入一个AGoner
-	a1        *AGoner `gone:"A1"` //具名注入A1
-	a2        *AGoner `gone:"A2"` //具名注入A2
-}
-

注意:上面代码,结构体属性后的标签:

  • gone:"*"是按类型的匿名注入
  • gone:"A1"是注入ID=A1的AGoner的具名注入

就是说,gone标签的值如果是*就是匿名注入;如果不是*,标签值就是要注入Goner的名字,也就是具名注入;当然,因为go是强类型的,所以无论 匿名注入 还是 具名注入 的 Goner 都必须是类型兼容的,否则注入失败。

# 指针注入、值注入 和 接口注入

如果被注入结构体的属性是一个指针,那么这个注入就是 指针注入值注入接口注入 的定义也是类似的。让我们来举个例子:

type AGoner struct {
-	gone.Flag //tell the framework that this struct is a Goner
-	Name      string
-}
-func (g *AGoner) Say() {
-	println("I am the AGoner, My name is", g.Name)
-}
-type Speaker interface {
-	Say()
-}
-type BGoner struct {
-	gone.Flag         //tell the framework that this struct is a Goner
-	a0         *AGoner `gone:"*"`  //匿名注入一个AGoner; 指针注入
-	a1        *AGoner `gone:"A1"` //具名注入A1; 指针注入
-	a2 AGoner  `gone:"A1"` // 值注入
-	a3 Speaker `gone:"A2"` // 接口注入
-}
-

上面代码中,BGoner.a0BGoner.a1指针注入BGoner.a2是值注入;BGoner.a3接口注入

需要特别提醒:“在go语言中,值类型 的赋值和传参都是传递的拷贝”,这意味着我们如果使用值注入时,实际上产生了一个新的“对象”,并且新旧对象只有在“传递那一刻”是相等,他们在内存中是独立的;这可能导致一些不符合“直觉”的结果,举个例子:

type BGoner struct {
-	gone.Flag
-	a1 AGoner  `gone:"A1"` // 值注入
-	a2 AGoner  `gone:"A1"` // 值注入
-}
-func (g *BGoner) AfterRevive() gone.AfterReviveError {
-	g.a1.Name = "dapeng"
-	g.a2.Name = "wang"
-	fmt.Printf("a1 is eq a2: %v", g.a1 == g.a2)
-	return nil
-}
-

在上面的代码中,BGoner.a1BGoner.a2 都被注入了 同一个 Goner(A1 ),但是因为是值注入,注入的过程中框架实际能做的只有将 A1 Goner 的值拷贝给了 BGoner.a1BGoner.a2BGoner.a1BGoner.a2被注入后,就和A1没有了任何联系,BGoner.a1BGoner.a2之间也没有联系,内存中也会有三份AGoner类型的空间占用;fmt.代码Printf("a1 is eq a2: %v", g.a1 == g.a2)打印的结果也会是 false

考虑到Gone作为基础框架更多意义只是提供可能性,所以我们保留了 值注入 功能;而大多数情况下,我们推荐使用 指针注入接口注入

# 指针注入 vs 接口注入

在Goner的Bury过程中,要求传递的是一个引用,即Cemetery.Bury方法的第一个参数必须是引用类型。指针注入和接口注入都可以将 Bury 的引用传递给结构体的属性。指针注入,简单直观,类型间一一对应,没有什么过多需要讲解的。接口(interface)做为go语言中最精华的设计之一,语言设计之初目的就在于 业务使用方和业务实现逻辑的解耦,让接口的使用方不用关注实现的细节。接口的另一个作用是解除循环依赖,如果两个模块间存在循环引用并且他们位于不同的包中;这样会导致package的循环依赖,在go语言中是禁止这样的行为的,编译阶段会失败。我们可以如下使用接口来解出package间的循环依赖。

循环依赖:
循环依赖

使用接口解除循环依赖:
使用接口解除循环依赖

使用接口能够隐藏业务逻辑的实现细节,能够有效的降低模块间的耦合,也更好的遵守“开放封闭”原则;因此,我们推荐使用 接口注入。但是万事没有绝对,引入接口一定会增加额外的成本,所以我们还是支持了 指针注入

# Slice注入和 Map注入

Gone 支持对SliceMap进行注入,即支持如下写法的:

type BGoner struct {
-	gone.Flag
-	aSlice1 []*AGoner `gone:"*"` //被注入的属性为Goner指针Slice
-	aSlice2 []AGoner  `gone:"*"` //被注入的属性为Goner值Slice
-	aMap1 map[string]*AGoner `gone:"*"` //被注入的属性为Goner指针的map
-	aMap2 map[string]AGoner  `gone:"*"` //被注入的属性为Goner值的map
-}
-

注入的规则如下:

    1. Slice 和 Map 的元素类型可以是 Goner指针类型 和 Goner的值类型,也可以是一个接口;
    1. Gone会将所有类型兼容的Goner注入到Slice 和 Map;
    1. Map key的类型只能是string;
    1. Map key的值为被注入Goner的GonerId,埋葬时没有指定GonerId的匿名Goner,Gone会自动生成一个Id。

注意

不推荐使用值作为Slice和Map的类型。

下面是完整的例子:

package main
+

# Goners 注册

为了完成依赖注入的自动装配,我们需要将所有的Goners注册到Gone框架中。下面给出上面公司依赖员工这个例子的完整代码,如下:

package main
 import (
 	"fmt"
 	"github.com/gone-io/gone"
 )
-type AGoner struct {
-	gone.Flag //tell the framework that this struct is a Goner
-	Name      string
-}
-func (g *AGoner) Say() string {
-	return fmt.Sprintf("I am the AGoner, My name is: %s", g.Name)
+type Employee interface {
+	Work()
+	Name() string
 }
-type BGoner struct {
+type Company struct {
 	gone.Flag
-	aSlice1 []*AGoner `gone:"*"` //被注入的属性为Goner指针Slice
-	aSlice2 []AGoner  `gone:"*"` //被注入的属性为Goner值Slice
-	aMap1 map[string]*AGoner `gone:"*"` //被注入的属性为Goner指针的map
-	aMap2 map[string]AGoner  `gone:"*"` //被注入的属性为Goner值的map
+	Boss Employee `gone:"boss"` // 标记需要依赖注入的属性
 }
-// AfterRevive executed After the Goner is revived; After `gone.Run`, gone framework detects the AfterRevive function on goners and runs it.
-func (g *BGoner) AfterRevive() gone.AfterReviveError {
-	for _, a := range g.aSlice1 {
-		fmt.Printf("aSlice1:%s\n", a.Say())
-	}
-	println("")
-	for _, a := range g.aSlice2 {
-		fmt.Printf("aSlice2:%s\n", a.Say())
-	}
-	println("")
-	for k, a := range g.aMap1 {
-		fmt.Printf("aMap1[%s]:%s\n", k, a.Say())
-	}
-	println("")
-	for k, a := range g.aMap2 {
-		fmt.Printf("aMap2[%s]:%s\n", k, a.Say())
-	}
-	return nil
+func (c *Company) Start() {
+	fmt.Printf("Company start, boss is %s\n", c.Boss.Name())
+	c.Boss.Work()
 }
-// NewA1 构造A1 AGoner
-func NewA1() (gone.Goner, gone.GonerId) {
-	return &AGoner{Name: "Injected Goner1"}, "A1"
+type EmployeeImpl struct {
+	gone.Flag
+	name string
 }
-// NewA2 构造A2 AGoner
-func NewA2() (gone.Goner, gone.GonerId) {
-	return &AGoner{Name: "Injected Goner2"}, "A2"
+func (e *EmployeeImpl) Work() {
+	fmt.Printf("I am working, my name is %s\n", e.Name())
 }
-func main() {
-	gone.Run(func(cemetery gone.Cemetery) error {
-		cemetery.
-			Bury(NewA1()).
-			Bury(&AGoner{Name: "Anonymous"}).
-			Bury(NewA2()).
-			Bury(&BGoner{})
-		return nil
-	})
+func (e *EmployeeImpl) Name() string {
+	return e.name
 }
-

例子执行的结果如下:

Revive github.com/gone-io/gone/heaven
-Revive github.com/gone-io/gone/cemetery
-Revive main/AGoner
-Revive main/AGoner
-Revive main/AGoner
-Revive main/BGoner
-aSlice1:I am the AGoner, My name is: Injected Goner1
-aSlice1:I am the AGoner, My name is: Anonymous
-aSlice1:I am the AGoner, My name is: Injected Goner2
-aSlice2:I am the AGoner, My name is: Injected Goner1
-aSlice2:I am the AGoner, My name is: Anonymous
-aSlice2:I am the AGoner, My name is: Injected Goner2
-aMap1[A1]:I am the AGoner, My name is: Injected Goner1
-aMap1[main/AGoner#1374393662624]:I am the AGoner, My name is: Anonymous
-aMap1[A2]:I am the AGoner, My name is: Injected Goner2
-aMap2[A2]:I am the AGoner, My name is: Injected Goner2
-aMap2[A1]:I am the AGoner, My name is: Injected Goner1
-aMap2[main/AGoner#1374393662624]:I am the AGoner, My name is: Anonymous
-

# 私有属性注入

根据“开发封闭”原则,模块依赖的属性应该是私有的,Gone支持对结构体的私有变量注入。

# 配置注入

可以看到,我们使用了cemetery.Bury 方法完成了Goner到Gone框架的注册;该方法第二参数可以指定被注册的Goner的Id,如果没有指定Id,Gone框架会自动为Goner随机生成一个Id。在具名注入时,我们会用到注册时的GonerId来作为gone标签的值。

# Priest函数

在上面代码中,gone.Prepare 方法接收的参数为一个匿名函数,它的形式如下:

func(cemetery Cemetery) error
+

在Gone框架中,这样形式的函数被定义为Priest函数,专门负责Goners的注册。

Priest的定义

type Priest func(cemetery Cemetery) error
+

大多数情况下,我们可以不用手动编写Priest代码;在gone中提供了 gone辅助工具 (opens new window),可以为我们自动生成Priest函数代码:自动生成Priest + (opens new window)

- + diff --git a/zh/guide/gone-vs-spring.html b/zh/guide/gone-vs-spring.html index b192610..eeda6f6 100644 --- a/zh/guide/gone-vs-spring.html +++ b/zh/guide/gone-vs-spring.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/goner-inject.html b/zh/guide/goner-inject.html index 6a44301..008bbc3 100644 --- a/zh/guide/goner-inject.html +++ b/zh/guide/goner-inject.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@ GitHub - (opens new window)

# Gone支持哪些方式注入?

# 共有属性注入 vs 私有属性注入

在Gone中,及支持共有属性注入也支持私有属性注入。依赖注入,一般情况依赖注入的“对象”都只应该局限于模块内部使用,基于“开放封闭”原则,我推荐使用私有属性注入。

提示

推荐私有属性注入

举个例子:

package demo
+     (opens new window)    

# Gone支持哪些方式注入?

# 公有属性注入 vs 私有属性注入

在Gone中,及支持共有属性注入也支持私有属性注入。依赖注入,一般情况依赖注入的“对象”都只应该局限于模块内部使用,基于“开放封闭”原则,我推荐使用私有属性注入。

提示

推荐私有属性注入

举个例子:

package demo
 import "github.com/gone-io/gone"
 type Worker struct {
 	gone.Flag // Goner标志,匿名内嵌`gone.Flag`表示该结构体为一个Goner
@@ -312,12 +312,12 @@
 slice map injected example result

- + diff --git a/zh/guide/hooks.html b/zh/guide/hooks.html index 2749b7a..3af85e4 100644 --- a/zh/guide/hooks.html +++ b/zh/guide/hooks.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/index.html b/zh/guide/index.html index 6cc2485..6bd41ca 100644 --- a/zh/guide/index.html +++ b/zh/guide/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/inner-goner.html b/zh/guide/inner-goner.html index 53cb677..54e80cd 100644 --- a/zh/guide/inner-goner.html +++ b/zh/guide/inner-goner.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/logrus.html b/zh/guide/logrus.html index bdd502a..c9e65d1 100644 --- a/zh/guide/logrus.html +++ b/zh/guide/logrus.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/redis.html b/zh/guide/redis.html index f664a69..3dabd77 100644 --- a/zh/guide/redis.html +++ b/zh/guide/redis.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/schedule.html b/zh/guide/schedule.html index b2c2ed7..e24d2cd 100644 --- a/zh/guide/schedule.html +++ b/zh/guide/schedule.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/tracer.html b/zh/guide/tracer.html index 0e1f0f8..6b35e27 100644 --- a/zh/guide/tracer.html +++ b/zh/guide/tracer.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/unit-test.html b/zh/guide/unit-test.html index 5b69643..f2cc890 100644 --- a/zh/guide/unit-test.html +++ b/zh/guide/unit-test.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/guide/xorm.html b/zh/guide/xorm.html index 3ac3517..2acf730 100644 --- a/zh/guide/xorm.html +++ b/zh/guide/xorm.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/index.html b/zh/index.html index cc031c5..aae1416 100644 --- a/zh/index.html +++ b/zh/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/gpt.html b/zh/quick-start/gpt.html index ba68f31..521046a 100644 --- a/zh/quick-start/gpt.html +++ b/zh/quick-start/gpt.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/index.html b/zh/quick-start/index.html index 1ddd5d8..be5bb38 100644 --- a/zh/quick-start/index.html +++ b/zh/quick-start/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/jwt+mysql+redis+schedule.html b/zh/quick-start/jwt+mysql+redis+schedule.html index 619ed73..cdc27bc 100644 --- a/zh/quick-start/jwt+mysql+redis+schedule.html +++ b/zh/quick-start/jwt+mysql+redis+schedule.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/mysql.html b/zh/quick-start/mysql.html index 58a8e8b..a070071 100644 --- a/zh/quick-start/mysql.html +++ b/zh/quick-start/mysql.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/simple.html b/zh/quick-start/simple.html index 10cb8c8..21e3903 100644 --- a/zh/quick-start/simple.html +++ b/zh/quick-start/simple.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/quick-start/web.html b/zh/quick-start/web.html index 9932506..62c5e34 100644 --- a/zh/quick-start/web.html +++ b/zh/quick-start/web.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/references/gone-tool.html b/zh/references/gone-tool.html index 785d396..d0a95ec 100644 --- a/zh/references/gone-tool.html +++ b/zh/references/gone-tool.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/references/http-inject.html b/zh/references/http-inject.html index ee270a1..72ceb40 100644 --- a/zh/references/http-inject.html +++ b/zh/references/http-inject.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/references/index.html b/zh/references/index.html index 1ec4048..2e5f106 100644 --- a/zh/references/index.html +++ b/zh/references/index.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/references/redis.html b/zh/references/redis.html index 66e1091..4a76f63 100644 --- a/zh/references/redis.html +++ b/zh/references/redis.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- + diff --git a/zh/references/xorm.html b/zh/references/xorm.html index ea6bf8d..7dea09d 100644 --- a/zh/references/xorm.html +++ b/zh/references/xorm.html @@ -21,7 +21,7 @@ } - + @@ -72,9 +72,9 @@
- +