Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8584191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:42:52+00:00 2026-06-11T21:42:52+00:00

Before I being a bit of background, I am very new to go programming

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:42:53+00:00Added an answer on June 11, 2026 at 9:42 pm

    Writing to the ResponseWriter (by calling Execute) before reading the value from the request is clearing it out.

    You can see this in action if you use this request handler:

    func GeneralHandler(w http.ResponseWriter, r *http.Request) {
       fmt.Println(r.Method)
       fmt.Println(r.URL)
       fmt.Println("before",r.FormValue("username"))
       index.Execute(w, nil)
       if r.Method == "POST" {
         fmt.Println("after",r.FormValue("username"))
       } 
     }
    

    This will print out before and after. However, in this case:

    func GeneralHandler(w http.ResponseWriter, r *http.Request) {
      fmt.Println(r.Method)
      fmt.Println(r.URL)
      index.Execute(w, nil)
      if r.Method == "POST" {
        fmt.Println("after",r.FormValue("username"))
      }
    }
    

    The after value will be blank.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Text gets accumulates piecemeal before being sent to client. Now we use own class
I have a class that requires a specific method to be called before being
We have a setup where most code, before being promoted to full production, is
I am risking this question being closed before i get an answer, but i
I know this issue being mentioned before, but resolutions there didn't apply. I'm having
Since the GOF book was put together well before .Net came into being, are
I've never come across this extension before. It's being used for a front-end interface
how can i get the parameters of any form being submitted with method=post before
The apply phase of save may fail and/or is still being done asynchronously before
I have a great deal of javascript that needs to be min'd before being

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.