Use OpenSSL generate a self-signed certificate for Golang. In this code snippet I created a certificate with validity of 15 years.
openssl genrsa -out server.key 2048
openssl req -new -x509 -sha256 -key server.key -out server.crt -days 5475
Once you create the certificate and key you can use it in your Golang HTTP like this
1package main
2
3import (
4 "log"
5 "net/http"
6 "time"
7
8 "github.com/julienschmidt/httprouter"
9)
10
11func main() {
12 router := httprouter.New()
13 router.HandlerFunc(http.MethodGet, "/", func(w http.ResponseWriter, r *http.Request) {
14 w.WriteHeader(200)
15 w.Write([]byte("Hello in TLS"))
16 })
17
18 httpServer := &http.Server{
19 Addr: ":8080",
20 Handler: router,
21 ReadTimeout: 120 * time.Second,
22 WriteTimeout: 120 * time.Second,
23 }
24
25 log.Fatal(httpServer.ListenAndServeTLS("server.crt", "server.key")) // <- Listen and serve with TLS
26}