Hy, I am newby in GOLANG but really want to understand GO better than I can. Today i work on a TCP server and I made a very basic server by localhost port 9090. Now I want to implement on this code the multithread using go routines. For example if the server is required twice or more at the same time, all I need it to run the html file at the same.
What code do you have now?
package main
import (
"fmt"
"html/template"
"log"
"net/http"
//"strings"
//"golang.org/x/crypto/openpgp/packet"
//"golang.org/x/net/context/ctxhttp"
)
func ma(w http.ResponseWriter, r *http.Request) {
//var pass string
fmt.Println("method:", r.Method) //get request method
if r.Method == "GET" {
t, _ := template.ParseFiles("ma.gtpl")
t.Execute(w, nil)
}
}
func login(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
t, _ := template.ParseFiles("login.gtpl")
t.Execute(w, nil)
}
}
func main() {
http.HandleFunc("/", ma)
http.HandleFunc("/login", login)
err := http.ListenAndServe(":9090", nil) // setting listening port
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
Now I want to implement on this code the multithread using go routines. For example if the server is required twice or more at the same time, all I need it to run the html file at the same.
You don’t have to do anything. The server provided by net/http
already launches a goroutine for each HTTP request.
EDIT:
You can format your code on this forum by putting ``` on a line before the code and the line after the code.
Thank you for your help, i am new here. Sorry for the ugly code that i added.
Welcome to the Go Forum!
Don’t worry about posting “ugly” code Seeing the code you have makes answering questions about it much easier.
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.