I am trying to implement a simple web server using Go. I expected "Hello World" to be displayed on the client's browser at the URL "http://127.0.0.1.12000/".
I have tried the following code but ended up with errors.
package main
import "net"
import "fmt"
import "bufio"
// import "strings"
// only needed below for sample processing
func main() {
fmt.Println("Launching server...")
// listen on all interfaces
ln, err := net.Listen("tcp", ":12000")
if err != nil {
fmt.Println("Launching error1...")
return
}
// run loop forever (or until ctrl-c)
for {
// accept connection on port
conn, err := ln.Accept()
if err != nil {
fmt.Println("Launching error2...")
return
}
// will listen for message to process ending in newline (
)
message, err := bufio.NewReader(conn).ReadString('
')
if err != nil {
fmt.Println("Launching error3...")
newmessage := "Hello World!"
conn.Write([]byte(newmessage + "
"))
return
}
// output message received
fmt.Print("Message Received:", string(message))
// sample process for string received
newmessage := "Hello World!"
conn.Write([]byte(newmessage + "
"))
}
}
When I tried to execute the code, the command line shows the following, but there is no output on browser..
Launching server...
Message Received:GET / HTTP/1.1
Message Received:GET / HTTP/1.1
Am I missing anything? Did I make any mistakes?