Server-Side Scripting/Iteration/Go

From Wikiversity
Jump to navigation Jump to search

server.go[edit | edit source]

// 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/

package main

import (
    "io"
    "net/http"
    "strconv"
)

var FORM = `
<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>
`

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

    result := ""

    switch request.Method {
        case "GET":
            result = FORM
        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
    }

    io.WriteString(response, result)
}

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
}

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

Try It[edit | edit source]

Copy and paste the code above into the following free online development environment or use your own Go compiler / interpreter / IDE.