Help us understand the problem. What is going on with this article?

Go Web Examplesの和訳(Forms)

More than 1 year has passed since last update.

Go勉強会 Webアプリケーション編 #2で使おうと思っているGo Web Examplesの和訳です。ほぼGoogle翻訳ですが、リンク等も整理された状態であると便利だと思い作りました。次回(Assets and Files)



// forms.go
package main

import (

type ContactDetails struct {
    Email   string
    Subject string
    Message string

func main() {
    tmpl := template.Must(template.ParseFiles("forms.html"))

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        if r.Method != http.MethodPost {
            tmpl.Execute(w, nil)

        details := ContactDetails{
            Email:   r.FormValue("email"),
            Subject: r.FormValue("subject"),
            Message: r.FormValue("message"),

        // do something with details
        _ = details

        tmpl.Execute(w, struct{ Success bool }{true})

    http.ListenAndServe(":8080", nil)
<!-- forms.html -->
{{if .Success}}
    <h1>Thanks for your message!</h1>
    <form method="POST">
        <label>Email:</label><br />
        <input type="text" name="email"><br />
        <label>Subject:</label><br />
        <input type="text" name="subject"><br />
        <label>Message:</label><br />
        <textarea name="message"></textarea><br />
        <input type="submit">
$ go run forms.go
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away