Golang写https服务端

版权声明:此文章转载自_infocool

原文链接:http://www.infocool.net/kb/Go/201609/188396.html

如需转载请联系听云College团队成员小尹 邮箱:yinhy#tingyun.com

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)
}
}


想阅读更多技术文章,请访问听云技术博客,访问听云官方网站感受更多应用性能优化魔力。

关于作者

coco秋洁

我爱学习,学习使我快乐

我要评论

评论请先登录,或注册