官术网_书友最值得收藏!

How to do it…

In this recipe, we are going to create a simple HTTP server that will render Hello World! when we browse http://localhost:8080 or execute curl http://localhost:8080 from the command line. Perform the following steps:

  1. Create http-server.go and copy the following content:
package main
import
(
"fmt"
"log"
"net/http"
)
const
(
CONN_HOST = "localhost"
CONN_PORT = "8080"
)
func helloWorld(w http.ResponseWriter, r *http.Request)
{
fmt.Fprintf(w, "Hello World!")
}
func main()
{
http.HandleFunc("/", helloWorld)
err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil)
if err != nil
{
log.Fatal("error starting http server : ", err)
return
}
}
  1. Run the program with the following command:
$ go run http-server.go
主站蜘蛛池模板: 汨罗市| 阿图什市| 始兴县| 南投县| 康定县| 绿春县| 施秉县| 杨浦区| 望谟县| 东光县| 西华县| 定边县| 长春市| 松滋市| 驻马店市| 专栏| 宁夏| 黔江区| 晋中市| 东乡县| 江油市| 扶绥县| 桑日县| 河曲县| 青浦区| 宣武区| 滕州市| 如皋市| 芦溪县| 丰城市| 环江| 古蔺县| 太湖县| 政和县| 龙江县| 西盟| 贡觉县| 滕州市| 瑞昌市| SHOW| 沁阳市|