HTTP:
package main
import (
"fmt"
"net/http"
)
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello world")
}
func main() {
http.HandleFunc("/demo",indexHandler)
http.ListenAndServe("127.0.0.1:80",nil)
}HTTPS:
创建证书:
openssl genrsa -out server.key 2048 openssl req -new -x509 -key server.key -out server.crt -days 365
代码:
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w,
"Hi, This is an example of https service in golang!")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServeTLS(":9090", "server.crt","server.key", nil)
}访问:
go run https.go curl -k https://127.0.0.1:9090