create a http server with a get route in go

Here's an example code snippet in Go that creates a HTTP server with a GET route:

main.go
package main

import (
	"fmt"
	"net/http"
)

func main() {
	http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprint(w, "Hello, world!")
	})

	http.ListenAndServe(":8080", nil)
}
208 chars
15 lines

In this example, we define a function that handles requests to the "/hello" route by writing "Hello, world!" as a response. We then use the ListenAndServe function from the http package to start the server on port 8080.

To run this code, save it in a file called main.go, and run go run main.go in your terminal. You can then visit http://localhost:8080/hello in your browser to see the response.

related categories

gistlibby LogSnag