二、实现接口路由</h2>

如果要在同一个请求路径下开发多个接口,示例如下:

<pre><code>接口要求: HTTP Method: get URL:请求路径/login HTTP Method: post URL:请求路径/text HTTP Method: post URL:请求路径/image HTTP Method: get URL:请求路径/logout </code></pre> <pre><code class="lang-go hljs"><span class="token keyword">type</span> router <span class="token keyword">struct</span> <span class="token punctuation">{</span> Route <span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>http<span class="token punctuation">.</span>HandlerFunc <span class="token punctuation">}</span> <span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">var</span> mRouter <span class="token operator">*</span>router <span class="token operator">=</span> <span class="token operator">&</span>router<span class="token punctuation">{</span><span class="token punctuation">}</span> mRouter<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"GET"</span><span class="token punctuation">,</span> <span class="token string">"/login"</span><span class="token punctuation">,</span> loginHandler<span class="token punctuation">)</span> mRouter<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"GET"</span><span class="token punctuation">,</span> <span class="token string">"/logout"</span><span class="token punctuation">,</span> logoutHandler<span class="token punctuation">)</span> mRouter<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"POST"</span><span class="token punctuation">,</span> <span class="token string">"/text"</span><span class="token punctuation">,</span> TextHandler<span class="token punctuation">)</span> mRouter<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"POST"</span><span class="token punctuation">,</span> <span class="token string">"/image"</span><span class="token punctuation">,</span> imageHandler<span class="token punctuation">)</span> err <span class="token operator">:=</span> http<span class="token punctuation">.</span><span class="token function">ListenAndServe</span><span class="token punctuation">(</span><span class="token string">"127.0.0.1:8081"</span><span class="token punctuation">,</span> mRouter<span class="token punctuation">)</span> <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span> fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"http listenAndServe failed:%v"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span> <span class="token punctuation">}</span> fmt<span class="token punctuation">.</span><span class="token function">Pringln</span><span class="token punctuation">(</span><span class="token string">"http listenAndServe success..."</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token keyword">func</span> <span class="token punctuation">(</span>rSelf <span class="token operator">*</span>router<span class="token punctuation">)</span> <span class="token function">ServeHTTP</span><span class="token punctuation">(</span>w http<span class="token punctuation">.</span>ResponseWriter<span class="token punctuation">,</span> r <span class="token operator">*</span>http<span class="token punctuation">.</span>Request<span class="token punctuation">)</span> <span class="token punctuation">{</span> mHandlerFunc<span class="token punctuation">,</span> succ <span class="token operator">:=</span> rSelf<span class="token punctuation">.</span>Route<span class="token punctuation">[</span>r<span class="token punctuation">.</span>Method<span class="token punctuation">]</span><span class="token punctuation">[</span>r<span class="token punctuation">.</span>URL<span class="token punctuation">.</span>Path<span class="token punctuation">]</span> <span class="token keyword">if</span> succ <span class="token operator">==</span> <span class="token boolean">true</span> <span class="token punctuation">{</span> <span class="token function">mHandlerFunc</span><span class="token punctuation">(</span>w<span class="token punctuation">,</span> r<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span> fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span><span class="token string">"no function handle this request,route failed..."</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">func</span> <span class="token punctuation">(</span>rSelf <span class="token operator">*</span>router<span class="token punctuation">)</span> <span class="token function">HandleFunc</span><span class="token punctuation">(</span>method<span class="token punctuation">,</span> path <span class="token builtin">string</span><span class="token punctuation">,</span> handleFunc http<span class="token punctuation">.</span>HandlerFunc<span class="token punctuation">)</span> <span class="token punctuation">{</span> method <span class="token operator">=</span> strings<span class="token punctuation">.</span><span class="token function">ToUpper</span><span class="token punctuation">(</span>method<span class="token punctuation">)</span> <span class="token keyword">if</span> rSelf<span class="token punctuation">.</span>Route <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span> rSelf<span class="token punctuation">.</span>Route <span class="token operator">=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>http<span class="token punctuation">.</span>HandlerFunc<span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token keyword">if</span> rSelf<span class="token punctuation">.</span>Route<span class="token punctuation">[</span>method<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span> rSelf<span class="token punctuation">.</span>Route<span class="token punctuation">[</span>method<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">make</span><span class="token punctuation">(</span><span class="token keyword">map</span><span class="token punctuation">[</span><span class="token builtin">string</span><span class="token punctuation">]</span>http<span class="token punctuation">.</span>HandlerFunc<span class="token punctuation">)</span> <span class="token punctuation">}</span> rSelf<span class="token punctuation">.</span>Route<span class="token punctuation">[</span>method<span class="token punctuation">]</span><span class="token punctuation">[</span>path<span class="token punctuation">]</span> <span class="token operator">=</span> handleFunc <span class="token punctuation">}</span> </code></pre>

下面这样不知道可不可以,没试:

<pre><code class="lang-go hljs"><span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span> http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"/login"</span><span class="token punctuation">,</span> loginHandler<span class="token punctuation">)</span> http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"/logout"</span><span class="token punctuation">,</span> logoutHandler<span class="token punctuation">)</span> http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"/text"</span><span class="token punctuation">,</span> textHandler<span class="token punctuation">)</span> http<span class="token punctuation">.</span><span class="token function">HandleFunc</span><span class="token punctuation">(</span><span class="token string">"/image"</span><span class="token punctuation">,</span> imageHandler<span class="token punctuation">)</span> err <span class="token operator">:=</span> http<span class="token punctuation">.</span><span class="token function">ListenAndServe</span><span class="token punctuation">(</span><span class="token string">"127.0.0.1:8081"</span><span class="token punctuation">,</span> <span class="token boolean">nil</span><span class="token punctuation">)</span> <span class="token keyword">if</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span> fmt<span class="token punctuation">.</span><span class="token function">Printf</span><span class="token punctuation">(</span><span class="token string">"http listenAndServe failed:%v"</span><span class="token punctuation">,</span> err<span class="token punctuation">)</span> <span class="token punctuation">}</span> fmt<span class="token punctuation">.</span><span class="token function">Pringln</span><span class="token punctuation">(</span><span class="token string">"http listenAndServe success..."</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> </code></pre> <h2>