使用go语言创建一个http服务器.
package main
import (
"fmt"
"log"
"net/http"
)
func myFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world!")
}
func main() {
http.HandleFunc("/hello", myFunc)
log.Fatal(http.ListenAndServe(":8080", nil))
}
使用go语言创建一个https服务端
package main
import (
"fmt"
"log"
"net/http"
)
func myFunc(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello world!")
}
func main() {
http.HandleFunc("/hello", myFunc)
log.Fatal(http.ListenAndServeTLS(":8443", "server.crt", "server.key", nil)) //其中 两个server文件需要预先生成并放到代码运行目录下
}
这篇博客介绍了如何使用Go语言搭建HTTP和HTTPS服务器。首先展示了如何创建一个简单的HTTP服务器,监听8080端口,然后进一步解释了如何配置HTTPS服务器,使用自签名的`server.crt`和`server.key`证书,监听8443端口。通过这两个实例,读者可以学习到Go语言在网络编程中的基本应用。

被折叠的 条评论
为什么被折叠?



