Switch case with multiple statements?

blov · · 344 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>Hey, anyone know how I could get this code to work:</p> <pre><code>func GetIps(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(routes) </code></pre> <p>}</p> <pre><code>func returnByIp(w http.ResponseWriter, r *http.Request) { var response []Route params := mux.Vars(r) for _, item := range routes { switch { case item.Level == params[&#34;level&#34;] &amp;&amp; item.Ip == params[&#34;ip&#34;]: response = append(response, item) case item.Level == params[&#34;level&#34;]: response = append(response, item) case item.Ip == params[&#34;ip&#34;]: response = append(response,item) case item.Smethod == params[&#34;smethod&#34;]: response = append(response, item) default : fmt.Println(&#34;Nothing found&#34;) } } if len(response) &gt; 0 { json.NewEncoder(w).Encode(response) return } </code></pre> <p>}</p> <p>Basically it works otherwise but when searching for a specific ip and level it will show all levels regardless as long as the ip is correct and vice versa (if level is correct it will show all ips) And I want the first case to only show results if both are correct.</p> <hr/>**评论:**<br/><br/>stone_henge: <pre><p>There are two main problems here:</p> <ol> <li>The switch statement is the wrong construct for this type of problem. You would better use an <code>if...else</code> sequence.</li> <li>You need to address both the concern of checking equality and checking whether the parameter is set at all.</li> </ol> <p>For reference, here is how I would solve it. It allows any combination of keys to be the search criteria, and setting no search keys returns all routes. There is some boiler plate for dummy data.</p> <pre><code>package main import &#34;encoding/json&#34; import &#34;os&#34; type Route struct { Ip string Level string Smethod string } func main() { var response []Route params := map[string]string{ &#34;level&#34;: &#34;5&#34;, } items := [...]Route{ Route{&#34;127.0.0.1&#34;, &#34;5&#34;, &#34;POST&#34;}, Route{&#34;10.0.0.2&#34;, &#34;7&#34;, &#34;GET&#34;}, Route{&#34;10.0.0.2&#34;, &#34;5&#34;, &#34;POST&#34;}, } check := func(set bool, pval string, ival string) bool { return !set || pval == ival } for _, item := range items { level, levelSet := params[&#34;level&#34;] ip, ipSet := params[&#34;ip&#34;] smethod, smethodSet := params[&#34;smethod&#34;] if check(ipSet, ip, item.Ip) &amp;&amp; check(levelSet, level, item.Level) &amp;&amp; check(smethodSet, smethod, item.Smethod) { response = append(response, item) } } enc := json.NewEncoder(os.Stdout) enc.Encode(response) } </code></pre></pre>anaerobic_lifeform: <pre><p>Your first case is basically useless; when the test fails, the other cases are tried. How do you differentiate between searching for both a level and an ip and only searching for one of them? My guess is that when you check for the level (resp. the ip), you should check if the ip (resp. the level) is empty.</p></pre>JonTargaryanTheFirst: <pre><p>Oh, in what way is it useless? I mean, does it not require both an level param and ip param or it will proceed to the next case? Is there something wrong with the formatting or? &#34;you should check if the ip (resp. the level) is empty.&#34;- This is exactly what I would like to achieve, however as I&#39;m still a newbie I don&#39;t really know how to do this. (Maybe with an if statement following the switch case?)</p></pre>anaerobic_lifeform: <pre><p>If the &#34;params&#34; map does not contain any &#34;ip&#34; key, the resulting string is the empty string &#34;&#34;.</p> <p>See for example: <a href="https://play.golang.org/p/F3qvyle1UW" rel="nofollow">https://play.golang.org/p/F3qvyle1UW</a></p> <p>You first check whether both level and ip matches; this fails. Then, you check if the level matches: if it does, then it does for any &#34;ip&#34;.</p> <p>I wouldn&#39;t add an if statement, just another condition:</p> <pre><code>case item.Level == params[&#34;level&#34;] &amp;&amp; params[&#34;ip&#34;] == &#34;&#34;: </code></pre></pre>JonTargaryanTheFirst: <pre><p>Thank you so much! I really appreciate it :)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

344 次点击  
加入收藏 微博
0 回复
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传