Web form question

func main() {
http.HandleFunc("/", form)
http.ListenAndServe(":8080", nil)
}

func form(res http.ResponseWriter, req *http.Request) {
value := req.FormValue(“text”)
res.Header().Set(“Content-Type”, “text/html; charset=utf-8”)
io.WriteString(res, <form method="POST"> <input type="text" name="text"> <input type="submit"> </form> <br>+value)

c := make(chan string)
c <- value

fmt.Println("check:", <-c)
fmt.Println("at the end")

}

Running above code, the http server just hang. Why is the form not able to be presented?

My intent is to get a user input before proceeding to the rest of the code. Actually intending to present another user input form after the first feedback(user input), but found out that both form will automatically be run at once, hence was thinking of using a channel to wait for first user input before gong for 2nd user input. But putting a channel hangs the server.

I’m very new to golang, and as new to do any coding. Is my understanding of http.Server wrong here? Isn’t the code supposed to run line by line?

Appreciate some enlightenment.

The channel c is unbuffered, so c <- value will not continue until there is another go routine reading from that channel.

That is why it “stales”.

In reply to your comments, I have 2 questions as it makes me more confusing.

  1. The channel statement is after the form statement. I supposed we have not come to channel statement, the form supposed to be presented right? I can’t even see the form.

  2. For channel c, I thought I read it immediately after that:
    fmt.Println(“check:”, <-c)

But still in the same main function. Does channel be only used in go routine? I cannot use in the same function?

Sorry, still trying to understand some basic concept.

Writing to HTTP responses is usually buffered and the current form is probably not filling the buffer, such it is not yet sent. This buffering happens in multiple layers, the res might be buffered, the runtime might have an additional buffer for streams between internal and external, the network stack of the operating system might have another buffer.

As I said it is not getting there, as it can not pass the unbuffered send.

Many thanks!

The http case is kind of look messy. Still not clear yet.

As for the channel, I kind of getting a bit of hints. Let me try out buffered?

Actually my main aim is to get 2 user input and present result at the end:

  1. web form - get data1
  2. depend on data1, run another web form, get data2
  3. use data1 and data2, present result at the end.

Is there a way to run 2 web forms in one handler to get 2 different user input, rather than calling separate handler to get different user input?

Nope, that is not how HTTP works.

A handler handles a single request. Not more, not less.

You need other means of transfering data from previous requests to another. A database is often used for longtime persisting data and KV stores like redis for short term persistence.

Oh oh … got it now. Many thanks.
Will try to learn database. Will also look up what is KV stores and redis mean.

Thank you very much for your help!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.