O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

  1. Open the console and create the folder chapter09/recipe12.
  2. Navigate to the directory.
  3. Place the created server.key and server.crt files in it.
  4. Create the file servetls.go with the following content:
        package main        import (          "fmt"          "net/http"        )        type SimpleHTTP struct{}          func (s SimpleHTTP) ServeHTTP(rw http.ResponseWriter,                              r *http.Request) {            fmt.Fprintln(rw, "Hello world")          }          func main() {            fmt.Println("Starting HTTP server on port 8080")            // Eventually you can use            // http.ListenAndServe(":8080", SimpleHTTP{})            s := &http.Server{Addr: ":8080", Handler: SimpleHTTP{}}            if err := s.ListenAndServeTLS("server.crt", "server.key");            err != nil {              panic(err)            }          }
  1. Execute the server by go run servetls.go.
  2. Access the URL https://localhost:8080 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required