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

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
主站蜘蛛池模板: 梁河县| 秦皇岛市| 临沭县| 宜昌市| 桓台县| 翼城县| 古浪县| 大丰市| 马公市| 铜鼓县| 宝坻区| 洞口县| 道真| 共和县| 焦作市| 凤冈县| 昌宁县| 甘洛县| 乃东县| 永登县| 泰来县| 江陵县| 古丈县| 鹤山市| 施秉县| 龙陵县| 吐鲁番市| 定远县| 莆田市| 旌德县| 遂川县| 洮南市| 温宿县| 泸西县| 南部县| 鱼台县| 安庆市| 泰顺县| 莱西市| 三江| 修水县|