英文:
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])+"
")
}
}
帮助我在getall
和get
函数中实现数组。
英文:
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 (
"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)
}
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) {
x := []Menu{
{Name: "Crispy", Price: 31},
{Name: "Burger", Price: 42},
{Name: "Superstar", Price: 17},
}
for _, m := range x {
io.WriteString(w, "<p>"+m.Name+" Price "+strconv.Itoa(m.Price)+"</p>")
}
}
func get(w http.ResponseWriter, r *http.Request) {
x := []Menu{
{Name: "Crispy", Price: 31},
{Name: "Burger", Price: 42},
{Name: "Superstar", Price: 17},
}
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>")
}
}
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论