Tutorial by Examples

package main import ( "log" "net/http" ) func main() { // Create a mux for routing incoming requests m := http.NewServeMux() // All URLs will be handled by this function m.HandleFunc("/", func(w http.ResponseWriter, r *http.Reque...
The typical way to begin writing webservers in golang is to use the standard library net/http module. There is also a tutorial for it here. The following code also uses it. Here is the simplest possible HTTP server implementation. It responds "Hello World" to any HTTP request. Save the...
HandleFunc registers the handler function for the given pattern in the server mux (router). You can pass define an anonymous function, as we have seen in the basic Hello World example: http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintln(w, "Hello, w...
Generate a certificate In order to run a HTTPS server, a certificate is necessary. Generating a self-signed certificate with openssl is done by executing this command: openssl req -x509 -newkey rsa:4096 -sha256 -nodes -keyout key.pem -out cert.pem -subj "/CN=example.com" -days 3650` T...
Responses can be written to a http.ResponseWriter using templates in Go. This proves as a handy tool if you wish to create dynamic pages. (To learn how Templates work in Go, please visit the Go Templates Documentation page.) Continuing with a simple example to utilise the html/template to respond ...
A simple static file server would look like this: package main import ( "net/http" ) func main() { muxer := http.NewServeMux() fileServerCss := http.FileServer(http.Dir("src/css")) fileServerJs := http.FileServer(http.Dir("src/js")) file...
Here are a simple example of some common tasks related to developing an API, differentiating between the HTTP Method of the request, accessing query string values and accessing the request body. Resources http.Handler interface http.ResponseWriter http.Request Available Method and Status cons...

Page 1 of 1