go programming POST FormValue can't be printed

Posted by poor_programmer on Stack Overflow See other posts from Stack Overflow or by poor_programmer
Published on 2012-09-27T00:31:18Z Indexed on 2012/09/27 3:37 UTC
Read the original article Hit count: 230

Filed under:

Before I being a bit of background, I am very new to go programming language. I am running go on Win 7, latest go package installer for windows. I'm not good at coding but I do like some challenge of learning a new language. I wanted to start learn Erlang but found go very interesting based on the GO I/O videos in youtube.

I'm having problem with capturing POST form values in GO. I spend three hours yesterday to get go to print a POST form value in the browser and failed miserably. I don't know what I'm doing wrong, can anyone point me to the right direction? I can easily do this in another language like C#, PHP, VB, ASP, Rails etc. I have search the entire interweb and haven't found a working sample. Below is my sample code.

Here is 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); //<-- this variable does not rendered in the browser!!!

    }
}

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)
}

Thanks!

PS: Very tedious to add four space before every line of code in stackoverflow especially when you are copy pasting. Didn't find it very user friendly or is there an easier way?

© Stack Overflow or respective owner

Related posts about go

  • Go import error while trying to import web.go package after using goinstall

    as seen on Stack Overflow - Search for 'Stack Overflow'
    With halfdans advice, I was successfully able to use goinstall github.com/hoisie/web.go without any errors after installing git first. However, now when I try to compile the sample code given, go is not finding the web package. I get the error, main.go:4: can't find import: web On this code package… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    The Oracle Develop conference (#oracledevelop10), being co-located for the first time ever with JavaOne in San Francisco, is guaranteed to be the ultimate rush for developers this year. Where else can you go to learn about, interact with, and meet fellow devotees of the entire Oracle Development… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    For those who don’t know, Oracle sponsors a group called “OWL” – Oracle Women’s Leadership - and the purpose of the group is to create local and global opportunities that support, educate and empower current and future women leaders at Oracle. This week, I had the opportunity to attend the Denver… >>> More

  • Go Big or Go Special

    as seen on SQL Team - Search for 'SQL Team'
    Watching Shark Tank tonight and the first presentation was by Mango Mango Preserves and it highlighted an interesting contrast in business trends today and how to capitalize on opportunities.  <Spoiler Alert> Even though every one of the sharks was raving about the product samples they tried… >>> More

  • juju bootstrap fails with a local environment, why?

    as seen on Ask Ubuntu - Search for 'Ask Ubuntu'
    Each time I try to bootstrap juju using a local enviroment it fails starting the juju-db-braiam-local script as follows: $ sudo juju --debug --verbose bootstrap 2013-10-20 02:28:53 INFO juju.provider.local environprovider.go:32 opening environment "local" 2013-10-20 02:28:53 DEBUG juju.provider.local… >>> More