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

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
主站蜘蛛池模板: 吐鲁番市| 社旗县| 尤溪县| 霍林郭勒市| 尚志市| 恭城| 苗栗县| 漳平市| 藁城市| 葫芦岛市| 永定县| 集贤县| 上虞市| 新蔡县| 龙江县| 民县| 鄯善县| 凤冈县| 咸宁市| 威宁| 昆山市| 禄丰县| 蛟河市| 新乡县| 巴东县| 崇左市| 石首市| 昌邑市| 包头市| 丰台区| 彝良县| 灵璧县| 彭水| 松桃| 涟源市| 塔城市| 潜山县| 元氏县| 灌云县| 太谷县| 通河县|