Need help with autocert package

I’m experimenting with autocert. I found something on internet:

package main

import (


func main() {
	m := autocert.Manager{
		Prompt:     autocert.AcceptTOS,
		HostPolicy: autocert.HostWhitelist("localhost"),
		Cache:      autocert.DirCache("certs"),

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello world"))

	s := &http.Server{
		Addr: ":https",
		TLSConfig: &tls.Config{
			GetCertificate: m.GetCertificate,

	s.ListenAndServeTLS("", "")

After running as root on Linux (Ubuntu 14.04) i get this error when i access from browser https://:localhost:

http: TLS handshake error from acme/autocert: server name component count invalid

Any suggestions?

Does your machine have a valid domain name? If so, whitelist this instead of localhost. Letsencrypt cannot issue certificates for localhost.

1 Like

Now i understand, indeed the machine don’t have a valid domain name. I was playing on my laptop.

How i resolved, some providers offer DDNS services (mine too :smile:) so i just activated some name on my internet connection and by forwarding 443 port to my laptop ip i was able to run and obtain valid cerificates from Let’s Encrypt. I also replaced autocert.HostWhitelist("localhost") with autocert.HostWhitelist(""). Can be useful when you don’t have a real payed domain name.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.