<p>This is my struct:</p>
<pre><code>type Route struct {
Ip string `json:"ip,omitempty,"`
Id int `json:"id,omitempty"`
Time time.Time `json:"time,omitempty"`
Spackage string `json:"spackage,omitempty"`
Smethod string `json:"smethod,omitempty"`
Level string `json:"level,omitempty"`
Message string `json:"message,omitempty"`
Data string `json:"data,omitempty"`
}
type Routes []Route
</code></pre>
<p>And this is my route create function:</p>
<pre><code>func RouteCreate(w http.ResponseWriter, r *http.Request) {
var route Route
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
panic(err)
}
if err := r.Body.Close(); err != nil {
panic(err)
}
if err := json.Unmarshal(body, &route); err != nil {
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusUnprocessableEntity) // Errorcode = unprocessable entity.
if err := json.NewEncoder(w).Encode(err); err != nil {
panic(err)
}
}
t := RepoCreateRoute(route)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusCreated)
if err := json.NewEncoder(w).Encode(t); err != nil {
panic(err)
}
}
</code></pre>
<p>This is my RepoCreateRoute function:</p>
<pre><code>func RepoCreateRoute(t Route) Route {
currentId += 1
t.Id = currentId
routes = append(routes, t)
return t
</code></pre>
<p>}</p>
<p>And this is my RepoFindRoute function:</p>
<pre><code>func RepoFindRoute(id int) Route {
for _, t := range routes {
if t.Id == id {
return t
}
}
return Route{} // Will return empty route.
</code></pre>
<p>}</p>
<p>// Now I would like to make a function that sorts the json to show only a specific IP that the user will input. I've been struggling with this for a couple of days now and it feels like I can't solve the issue (I'm quite new to GO and coding as a whole)</p>
<hr/>**评论:**<br/><br/>Lord_Peppe: <pre><p>Sorts? Or filters routes to return one or more routes that contain a searched for IP? </p>
<p>Make something like your RepoFindRoute, but search by the IP field. If you want to return multiple routes, don't return when searching the routes array make a array/slice of matching values and return that at the end.</p></pre>JonTargaryanTheFirst: <pre><p>Thank you, i did like you said and now its working (almost). It doesnt show more then one result when searching for a specific ip :/</p></pre>Redundancy_: <pre><p>I'd recommend writing a Go playground example / unit test for this kind of thing, which can help clarify what you mean when you say something like "sorts the json".</p></pre>JonTargaryanTheFirst: <pre><p>I'll do that for sure once I get to work on monday :)</p></pre>Lord_Peppe: <pre><p>When you found your matching IP did you do what you did in your ID search and return (ending the loop) or did you add it to an array/slice and then return that slice when the loop finished?</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传