Golang写https服务端
1. 生成私钥
openssl genrsa -out key.pem 2048
2. 生成证书
openssl req -new -x509 -key key.pem -out cert.pem -days 1095
3. 服务端代码:
package main
import (
"fmt"
"log"
"net/http"
)
func test(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "this is a test for you")
}
func main() {
http.HandleFunc("/test", test)
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
if err != nil {
log.Fatal("ListenerAndServe:", err)
}
}