Server-Side Scripting/Iteration/Go

lesson4.go

edit
// Demonstrates conditions, while loops and for loops using
// Celsius and Fahrenheit temperature conversion tables.
//
// References:
//  https://www.mathsisfun.com/temperature-conversion.html
//  https://golang.org/doc/
//  https://golangcode.com/get-a-url-parameter-from-a-request/
//  https://gowebexamples.com/
//  https://tour.golang.org/
//  https://pkg.go.dev/html/template

package routes

import (
    "net/http"
    "html/template"
    "path/filepath"
	"strconv"
)

func Lesson4(response http.ResponseWriter, request *http.Request) {
	response.Header().Set("Content-Type", "text/html; charset=utf-8")

	type Data struct {
		Table  template.HTML
	}
	
	result := ""

    switch request.Method {
        case "GET":
            result = ""
        case "POST":
            start, _ := strconv.Atoi(request.FormValue("start"))
            stop, _ := strconv.Atoi(request.FormValue("stop"))
            increment, _ := strconv.Atoi(request.FormValue("increment"))
            submit := request.FormValue("submit")

            if submit == "Celsius" {
                result = processCelsius(start, stop, increment)
            } else if submit == "Fahrenheit" {
                result = processFahrenheit(start, stop, increment)
            } else {
                result = "Unexpected submit value: " + submit
            }
        default:
            result = "Unexpected request method: " + request.Method
        
    }

	data := Data{template.HTML(result)}
	path := filepath.Join("templates", "lesson4.html")
	parsed, _ := template.ParseFiles(path)
	parsed.Execute(response, data)
}

func processCelsius(start int, stop int, increment int) string {
    result := "<h1>Temperature Conversion</h1>"
    result = result + "<table><tr><th>Celsius</th><th>Fahrenheit</th></tr>"
    celsius := start
    for celsius <= stop {
        fahrenheit := float64(celsius) * 9 / 5 + 32
        result += "<tr><td>" + strconv.Itoa(celsius) + "</td>"
        result += "<td>" + strconv.FormatFloat(fahrenheit, 'f', 1, 64) + "</td></tr>"
        celsius += increment
    }
    result += "</table>"
    return result
}

func processFahrenheit(start int, stop int, increment int) string {
    result := "<h1>Temperature Conversion</h1>"
    result = result + "<table><tr><th>Fahrenheit</th><th>Celsius</th></tr>"
    for fahrenheit := start; fahrenheit <= stop; fahrenheit += increment {
        temp := float64(fahrenheit)
        celsius := (float64(temp) - 32) * 5 / 9
        result += "<tr><td>" + strconv.Itoa(fahrenheit) + "</td>"
        result += "<td>" + strconv.FormatFloat(celsius, 'f', 1, 64) + "</td></tr>"
    }
    result += "</table>"
    return result
}

lesson4.html

edit
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Lesson 4</title>
    <link rel="stylesheet" href="styles.css">
    <style>
        p {
            min-height: 1em;
        }
    </style>
</head>

<body>
    <h1>Temperature Conversion</h1>
    <form method="POST">
        <p><label for="start">Start:</label>
            <input type="text" id="start" name="start" value="0">
        </p>
        <p><label for="stop">Stop:</label>
            <input type="text" id="stop" name="stop" value="100">
        </p>
        <p><label for="increment:">Increment</label>
            <input type="text" id="increment" name="increment" value="10">
        </p>
        <p><input type="submit" name="submit" value="Celsius">
            <input type="submit" name="submit" value="Fahrenheit">
        </p>
    </form>
    {{.Table}}
</body>

</html>

Try It

edit

See Server-Side Scripting/Routes and Templates/Go to create a test environment.