Good day! In this program, I make the menu for restaurant including Handle function.I can't connect array: Name, Price with functions getall and get

huangapple go评论81阅读模式
英文:

Good day! In this program, I make the menu for restaurant including Handle function.I can't connect array: Name, Price with functions getall and get

问题

package main

import (
"fmt"
"net/http"
"io"
"strconv"
"net/url"
)

type Menu struct {
Name string
Price int
description string
}

func (m Menu) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Price, )
}

func main() {
x :=[]Menu{
{Name:"Crispy", Price:31},
{Name:"Burger", Price:42},
{Name:"Superstar",Price:17},
/{"Cola", 26},
{Name:"Superstar", Price:40},
{Name:"Nuggets", Price:19},
/
}
//getall(&x)
fmt.Println(x)

http.HandleFunc("/getall", getall)
http.HandleFunc("/get", get)
http.ListenAndServe(":8080", nil)

}

func getall(w http.ResponseWriter, r *http.Request) {
for _, m := range []Menu{}{
io.WriteString(w, "

"+m.Name+" Price "+strconv.Itoa(m.Price)+"

")
}
}

func get(w http.ResponseWriter, r *http.Request) {
url := url.URL{}
println(url.String())
attr := r.URL.Query()
fmt.Println(attr)
id := attr["id"][0]
fmt.Println("id of request " + id)
v := id
d:= []Menu{}
if a, err := strconv.Atoi(v);
err == nil {
io.WriteString(w, "

"+Name[a-1]+" price "+strconv.Itoa(d[a-1])+"

")
}
}

帮助我在getallget函数中实现数组。

英文:

Good day! In this program, I make the menu for restaurant including Handle function.

Problem Statement:
I can't connect array: Name, Price with functions getall and get.

package main

import (
	"fmt"
	"net/http"
	"io"
	"strconv"
	"net/url"
)

type Menu struct {
	Name string
	Price  int
	description string
}

func  (m Menu) String() string {
    return fmt.Sprintf("%s: %s", m.Name, m.Price, )
}

func main() {
	x :=[]Menu{
		{Name:"Crispy", Price:31},
		{Name:"Burger", Price:42},
		{Name:"Superstar",Price:17},
		/*{"Cola", 26},
		{Name:"Superstar", Price:40},
		{Name:"Nuggets", Price:19},*/
	}
	//getall(&x)
	fmt.Println(x)

	http.HandleFunc("/getall", getall)
	http.HandleFunc("/get", get)
	http.ListenAndServe(":8080", nil)
}

func getall(w http.ResponseWriter, r *http.Request) {
	for _,num := range []Menu{}{
		io.WriteString(w, "<p>"+m.Name+" Price "+strconv.Itoa(num)+"</p>")
	}
}

func get(w http.ResponseWriter, r *http.Request) {
	url := url.URL{}
	println(url.String())
	attr := r.URL.Query()
	fmt.Println(attr)
	id := attr["id"][0]
	fmt.Println("id of request " + id)
	v := id
	d:= []Menu{}
	if a, err := strconv.Atoi(v);
	err == nil {
		io.WriteString(w, "<p> "+Name[a-1]+" price "+strconv.Itoa(d[a-1])+"</p>")
	}
}

Help me to implement array in getall and get function.

答案1

得分: 0

我很乐意帮助你,因为你似乎真的在努力学习Go语言。我对你的代码进行了一些更改。我将菜单列表移动到了每个处理程序中,你可以将其保持为全局变量。以下是修改后的代码:

package main

import (
	"fmt"
	"io"
	"net/http"
	"net/url"
	"strconv"
)

type Menu struct {
	Name        string
	Price       int
	description string
}

func (m Menu) String() string {
	return fmt.Sprintf("%s: %s", m.Name, m.Price)
}

var x = []Menu{
	{Name: "Crispy", Price: 31},
	{Name: "Burger", Price: 42},
	{Name: "Superstar", Price: 17},
}

func main() {
	fmt.Println(x)

	http.HandleFunc("/getall", getall)
	http.HandleFunc("/get", get)
	http.ListenAndServe(":8080", nil)
}

func getall(w http.ResponseWriter, r *http.Request) {
	for _, m := range x {
		io.WriteString(w, "<p>"+m.Name+" Price "+strconv.Itoa(m.Price)+"</p>")
	}
}

func get(w http.ResponseWriter, r *http.Request) {
	url := url.URL{}
	println(url.String())
	attr := r.URL.Query()
	fmt.Println(attr)
	id := attr["id"][0]
	fmt.Println("id of request " + id)
	v := id
	if a, err := strconv.Atoi(v); err == nil {
		io.WriteString(w, "<p> "+x[a-1].Name+" price "+strconv.Itoa(x[a-1].Price)+"</p>")
	}
}

尝试访问:http://localhost:8080/getall 或 http://localhost:8080/get?id=1

英文:

I would like to help you as it seems you are genuinely trying to learn go. I've made few changes in your code. I've moved the menu listing in each handler. You may keep it global. Find the changed code below.

package main
import (
&quot;fmt&quot;
&quot;io&quot;
&quot;net/http&quot;
&quot;net/url&quot;
&quot;strconv&quot;
)
type Menu struct {
Name        string
Price       int
description string
}
func (m Menu) String() string {
return fmt.Sprintf(&quot;%s: %s&quot;, m.Name, m.Price)
}
func main() {
fmt.Println(x)
http.HandleFunc(&quot;/getall&quot;, getall)
http.HandleFunc(&quot;/get&quot;, get)
http.ListenAndServe(&quot;:8080&quot;, nil)
}
func getall(w http.ResponseWriter, r *http.Request) {
x := []Menu{
{Name: &quot;Crispy&quot;, Price: 31},
{Name: &quot;Burger&quot;, Price: 42},
{Name: &quot;Superstar&quot;, Price: 17},
}
for _, m := range x {
io.WriteString(w, &quot;&lt;p&gt;&quot;+m.Name+&quot; Price &quot;+strconv.Itoa(m.Price)+&quot;&lt;/p&gt;&quot;)
}
}
func get(w http.ResponseWriter, r *http.Request) {
x := []Menu{
{Name: &quot;Crispy&quot;, Price: 31},
{Name: &quot;Burger&quot;, Price: 42},
{Name: &quot;Superstar&quot;, Price: 17},
}
url := url.URL{}
println(url.String())
attr := r.URL.Query()
fmt.Println(attr)
id := attr[&quot;id&quot;][0]
fmt.Println(&quot;id of request &quot; + id)
v := id
if a, err := strconv.Atoi(v); err == nil {
io.WriteString(w, &quot;&lt;p&gt; &quot;+x[a-1].Name+&quot; price &quot;+strconv.Itoa(x[a-1].Price)+&quot;&lt;/p&gt;&quot;)
}
}

Try visiting: http://localhost:8080/getall OR http://localhost:8080/get?id=1

答案2

得分: 0

你可以使用以下代码来实现getall()函数:

for m := range menu {
    // 实现逻辑
}

而对于get()函数,你可以使用以下代码:

for i, _ := range menu {
    if i == expect {
        // 实现逻辑
    }
}

希望对你有帮助!如果还有其他问题,请随时提问。

英文:

user for m :=range menu{...} to implement getall(), use for i,_ := range menu{ if i == espect {...}} to implement get()

huangapple
  • 本文由 发表于 2017年7月11日 16:55:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/45029816.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定