Golang实现包含菜单、增删功能的Web应用示例
Golang实现包含菜单、增删功能的Web应用示例 下午好,
希望论坛里有人能理解并书写西班牙语。
我想告诉大家,我刚开始学习Go语言编程,因此,如果可能的话,我请求有人能帮助我更好地理解Go编程环境,并能提供一个使用菜单、增删操作的Web应用程序示例。
非常感谢。
此致, Esteban Méndez 墨西哥
更多关于Golang实现包含菜单、增删功能的Web应用示例的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
// Ejemplo de aplicación web en Go con menú y operaciones CRUD package main
import ( “fmt” “html/template” “net/http” “strconv” )
// Estructura para los elementos del menú type MenuItem struct { ID int Name string Price float64 }
var menuItems = []MenuItem{ {1, “Tacos al Pastor”, 25.50}, {2, “Enchiladas”, 32.00}, {3, “Chiles Rellenos”, 28.75}, }
var templates = template.Must(template.ParseGlob(“templates/*.html”))
func main() { http.HandleFunc("/", indexHandler) http.HandleFunc("/add", addHandler) http.HandleFunc("/delete", deleteHandler) http.HandleFunc("/edit", editHandler)
fmt.Println("Servidor iniciado en http://localhost:8080")
http.ListenAndServe(":8080", nil)
}
func indexHandler(w http.ResponseWriter, r *http.Request) { data := struct { Title string Items []MenuItem }{ Title: “Menú del Restaurante”, Items: menuItems, } templates.ExecuteTemplate(w, “index.html”, data) }
func addHandler(w http.ResponseWriter, r *http.Request) { if r.Method == “POST” { name := r.FormValue(“name”) price, _ := strconv.ParseFloat(r.FormValue(“price”), 64)
newID := len(menuItems) + 1
newItem := MenuItem{
ID: newID,
Name: name,
Price: price,
}
menuItems = append(menuItems, newItem)
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
templates.ExecuteTemplate(w, "add.html", nil)
}
func deleteHandler(w http.ResponseWriter, r *http.Request) { if r.Method == “POST” { id, _ := strconv.Atoi(r.FormValue(“id”))
for i, item := range menuItems {
if item.ID == id {
menuItems = append(menuItems[:i], menuItems[i+1:]...)
break
}
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
id, _ := strconv.Atoi(r.URL.Query().Get("id"))
templates.ExecuteTemplate(w, "delete.html", id)
}
func editHandler(w http.ResponseWriter, r *http.Request) { if r.Method == “POST” { id, _ := strconv.Atoi(r.FormValue(“id”)) name := r.FormValue(“name”) price, _ := strconv.ParseFloat(r.FormValue(“price”), 64)
for i, item := range menuItems {
if item.ID == id {
menuItems[i].Name = name
menuItems[i].Price = price
break
}
}
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
id, _ := strconv.Atoi(r.URL.Query().Get("id"))
var itemToEdit MenuItem
for _, item := range menuItems {
if item.ID == id {
itemToEdit = item
break
}
}
templates.ExecuteTemplate(w, "edit.html", itemToEdit)
}
// templates/index.html /*
<!DOCTYPE html> <html> <head> <title>{{.Title}}</title> </head> <body>{{.Title}}
Agregar nuevo item </body> </html> */// templates/add.html /*
<!DOCTYPE html> <html> <body>Agregar nuevo item
<form method="POST"> Nombre: <input type="text" name="name" required>Precio: <input type="number" step="0.01" name="price" required>
<button type="submit">Guardar</button> </form> Volver al menú </body> </html> */
// templates/delete.html /*
<!DOCTYPE html> <html> <body>Confirmar eliminación
<form method="POST"> <input type="hidden" name="id" value="{{.}}">¿Está seguro de eliminar este item?
<button type="submit">Eliminar</button> </form> Cancelar </body> </html> */// templates/edit.html /*
<!DOCTYPE html> <html> <body>Editar item
<form method="POST"> <input type="hidden" name="id" value="{{.ID}}"> Nombre: <input type="text" name="name" value="{{.Name}}" required>Precio: <input type="number" step="0.01" name="price" value="{{.Price}}" required>
<button type="submit">Actualizar</button> </form> Cancelar </body> </html> */

