Understanding Go's net package-Day 2-

Introduction

This article is the second day article of Understanding Go's net package-Day 1-.

This time, ** POST and GET processing and html output ** will be performed.

Source code

main.go


package main

import (
	"fmt"
	"log"
	"net/http"
	"text/template"
	"time"
)

type Host struct {
	Title string
	Date  time.Time
	Text  string
}

func main() {
	http.HandleFunc("/", handler)
	http.HandleFunc("/temp", handlerTemp)
	http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {
	switch r.Method {
	case "GET":
		fmt.Fprint(w, "Hello GET")
	case "POST":
		fmt.Fprint(w, "Hello POST")
	default:
		fmt.Fprint(w, "Hello")
	}
}

func handlerTemp(w http.ResponseWriter, r *http.Request) {
	t, err := template.ParseFiles("template/index.html")
	if err != nil {
		log.Fatalf("template ERROR")
	}
	host := Host{
		Title: "Test title",
		Date:  time.Now(),
		Text:  "This is a practice for net packages.",
	}

	t.Execute(w, host)
}

index.html


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    Test Page
    <form method="POST" action="/">
        <button type="submit">POST</button>
    </form>

    <p>{{ .Title }}</p>
    <p>{{ .Date }}</p>
    <p>{{ .Text }}</p>
</body>
</html>

I created it by quoting the previous code. I will explain each function one by one.

main function

This is almost the same as last time. The server is starting on port 8080. I added / temp

handler function

It has changed significantly from the last time. The method that is skipped to handler is handled by the switch statement.

This way of writing works normally even if the same function is used as an argument because the methods are different. (Somehow it feels cool)

This time, I wanted to check the method for /, so I wrote it like this.

handlerTemp function

This liver function. Here, we are processing the html output system.

First, declare the variable t and specifytemplate.ParseFiles ("template/index.html").

This will output index.html in the template directory.

As you can see in the code of index.html, we handle templates like{{.hoge}}, so we create and define Host as a structure.

The last t.Execute returns host

Try to run

Try to access /

image.png

The print statement is Hello GET because of the GET method

Try to access/temp

image.png

You can see that the contents of host are output. Now try accessing / with the POST button

image.png Since I accessed it by POST, I can see that the print statement is Hello POST.

Summary

When I made something with Go, I used to develop applications using gin and echo, but since the net package can be used effectively for purposes other than application development, I will continue to write it as a memo.

net package. It feels pretty good.

Recommended Posts

Understanding Go's net package-Day 1-
Understanding Go's net package-Day 2-