A simple web server
The first thing our chat application needs is a web server that has two main responsibilities:
Serving the HTML and JavaScript chat clients that run in the user's browser
Accepting web socket connections to allow the clients to communicate
Note
The GOPATH environment variable is covered in detail in Appendix, Good Practices for a Stable Go environment. Be sure to read that first if you need help getting set up.
Create a main.go file inside a new folder called chat in your GOPATH and add the following code:
package main
import (
"log"
"net/http"
)
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`))
<html>
<head>
<title>Chat</title>
</head>
<body>
Let's chat!
</body>
</html>
))
})
// start the web server
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal("ListenAndServe...