Before I overdo it a bit, I am very new to programming. I am running Win 7, the latest version of installing packages for windows. I'm not good at coding, but I really like calling a new language. I wanted to start learning Erlang, but found it very interesting based on a GO I / O video on YouTube.
I am having trouble displaying the values ββof the POST form in GO. I spend three hours yesterday to start typing the value of the POST form in the browser and failed. I don't know what I'm doing wrong, can someone point me in the right direction? I can easily do this in another language such as C #, PHP, VB, ASP, Rails, etc. I have a search all over interweb and no working sample has been found. The following is sample code.
Here is the Index.html page
{{ define "title" }}Homepage{{ end }}
{{ define "content" }}
<h1>My Homepage</h1>
<p>Hello, and welcome to my homepage!</p>
<form method="POST" action="/">
<p> Enter your name : <input type="text" name="username"> </P>
<p> <button>Go</button>
</form>
<br /><br />
{{ end }}
Here is the base page
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ template "title" . }}</title>
</head>
<body>
<section id="contents">
{{ template "content" . }}
</section>
<footer id="footer">
My homepage 2012 copy
</footer>
</body>
</html>
now some go code
package main
import (
"fmt"
"http"
"strings"
"html/template"
)
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func GeneralHandler(w http.ResponseWriter, r *http.Request) {
index.Execute(w, nil)
if r.Method == "POST" {
a := r.FormValue("username")
fmt.Fprintf(w, "hi %s!",a);
}
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
remPartOfURL := r.URL.Path[len("/hello/"):]
fmt.Fprintf(w, "Hello %s!", remPartOfURL)
}
func main() {
http.HandleFunc("/", GeneralHandler)
http.HandleFunc("/hello/", helloHandler)
http.ListenAndServe("localhost:81", nil)
}
Thank!
PS: It is very tedious to add four spaces before each line of code in stackoverflow, especially when you copy the insert. Didn't find it very user friendly or is there an easier way?