Create web server
To create a web server in the Go programming language, you can follow these steps:
- Import necessary packages:
package main
import (
"fmt"
"net/http"
)
- Create a handler function to handle incoming requests:
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, you've requested: %s\n", r.URL.Path)
}
- In the main function, set up the server to listen on a specific port and route requests to the handler function:
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
By following these steps, you can create a simple web server in Go that responds to incoming HTTP requests.