<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["level"] && item.Ip == params["ip"]:
response = append(response, item)
case item.Level == params["level"]:
response = append(response, item)
case item.Ip == params["ip"]:
response = append(response,item)
case item.Smethod == params["smethod"]:
response = append(response, item)
default : fmt.Println("Nothing found")
}
}
if len(response) > 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 "encoding/json"
import "os"
type Route struct {
Ip string
Level string
Smethod string
}
func main() {
var response []Route
params := map[string]string{
"level": "5",
}
items := [...]Route{
Route{"127.0.0.1", "5", "POST"},
Route{"10.0.0.2", "7", "GET"},
Route{"10.0.0.2", "5", "POST"},
}
check := func(set bool, pval string, ival string) bool {
return !set || pval == ival
}
for _, item := range items {
level, levelSet := params["level"]
ip, ipSet := params["ip"]
smethod, smethodSet := params["smethod"]
if check(ipSet, ip, item.Ip) &&
check(levelSet, level, item.Level) &&
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? "you should check if the ip (resp. the level) is empty."- This is exactly what I would like to achieve, however as I'm still a newbie I don't really know how to do this. (Maybe with an if statement following the switch case?)</p></pre>anaerobic_lifeform: <pre><p>If the "params" map does not contain any "ip" key, the resulting string is the empty string "".</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 "ip".</p>
<p>I wouldn't add an if statement, just another condition:</p>
<pre><code>case item.Level == params["level"] && params["ip"] == "":
</code></pre></pre>JonTargaryanTheFirst: <pre><p>Thank you so much! I really appreciate it :)</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传