package main import ( "fmt" "html/template" "net/http" "time" ) func main() { // Главная страница http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { tmpl := template.Must(template.ParseFiles("template/index.html")) tmpl.Execute(w, nil) }) // 1. Возвращаем фрагмент времени http.HandleFunc("/get-time", func(w http.ResponseWriter, r *http.Request) { currentTime := time.Now().Format("15:04:05") fmt.Fprintf(w, "Текущее время: %s", currentTime) }) // 2. Обработка формы (возвращаем только новый элемент списка) http.HandleFunc("/add-item", func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) return } item := r.FormValue("item") // Возвращаем фрагмент
  • fmt.Fprintf(w, "
  • %s (добавлено в %s)
  • ", item, time.Now().Format("15:04")) }) // 3. Удаление (возвращаем пустоту или сообщение об успехе) http.HandleFunc("/delete-danger", func(w http.ResponseWriter, r *http.Request) { if r.Method == http.MethodDelete { w.WriteHeader(http.StatusOK) fmt.Fprintf(w, "
    Объект успешно аннигилирован
    ") } }) fmt.Println("Сервер запущен на :8080...") http.ListenAndServe(":8080", nil) }