Showing posts with label Addition of Two Numbers in Golang Using Web Forms. Show all posts
Showing posts with label Addition of Two Numbers in Golang Using Web Forms. Show all posts

Thursday, October 3, 2019

Addition of Two Numbers in Golang Using Web Forms

A simple addition of two numbers in golang using web forms that I wrote while learning web development in Go programming language.

I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.

Here in Bacolod I also accepting computer repair, networking and Arduino Project development at a very affordable price.


My personal website is http://www.jakerpomperada.com.



Program Listing

index.tmpl

{{ define "Index" }}
<!DOCTYPE html>
<html lang="en-US">
    <head>
        <title>Add</title>
        <meta charset="UTF-8" />
    </head>
<body>
    <form method="POST" action="/Add">
     <br>
     <h2>  Addition of Two Numbers in Golang </h2>
     <br><br>
     First Value <input type="text" name="FNumber" value="{{.FNumber}}"/><br />
     Second Value  <input type="text" name="SNumber" value="{{.SNumber }}"/><br /><br>
     Total Sum  <input type="text" name="Total" value="{{ .Total }}"/><br />
      <br>
      <input type="submit" value="Compute" />
        &nbsp;
        <input type="reset" value="Reset">
       
    </form><br />   
</body>
</html>     
{{ end }}


add.go

package main 


import( "strconv"
"net/http"
"text/template"
)

type Sum struct {
FNumber string
SNumber string
Total string
}

var tmpl = template.Must(template.ParseGlob("templates/*"))

func Add(w http.ResponseWriter, r *http.Request) {
r.ParseForm()                     
    fNumber, err := strconv.Atoi(r.Form.Get("FNumber"))
    if err != nil {
    panic(err)
}
    sNumber, err := strconv.Atoi(r.Form.Get("SNumber"))
    if err != nil {
    panic(err)
}

    total := fNumber + sNumber

    sum := Sum{}
    sum.FNumber = r.Form.Get("FNumber")
    sum.SNumber = r.Form.Get("SNumber")
    sum.Total = strconv.Itoa(total)
    tmpl.ExecuteTemplate(w, "Index", sum)
}

func Index(w http.ResponseWriter, r *http.Request) {
    tmpl.ExecuteTemplate(w, "Index", Sum{})
}

func main() {
  http.HandleFunc("/", Index)
  http.HandleFunc("/Add", Add)
    http.ListenAndServe(":8080", nil)

}