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

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
主站蜘蛛池模板: 右玉县| 大姚县| 旌德县| 新乡市| 汝南县| 教育| 海盐县| 远安县| 平邑县| 贵定县| 高尔夫| 杭锦后旗| 池州市| 泉州市| 张家港市| 邹平县| 玛纳斯县| 新源县| 全椒县| 沾化县| 华阴市| 汝阳县| 崇明县| 江津市| 万年县| 镇宁| 安岳县| 即墨市| 栾城县| 盐边县| 定襄县| 平度市| 油尖旺区| 泰安市| 西藏| 济阳县| 子长县| 穆棱市| 读书| 上杭县| 天柱县|