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

How to do it…

In this recipe, we will define three routes, such as /, /login, and /logout along with their handlers. Perform the following steps:

  1. Create http-server-basic-routing.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 login(w http.ResponseWriter, r *http.Request)
{
fmt.Fprintf(w, "Login Page!")
}
func logout(w http.ResponseWriter, r *http.Request)
{
fmt.Fprintf(w, "Logout Page!")
}
func main()
{
http.HandleFunc("/", helloWorld)
http.HandleFunc("/login", login)
http.HandleFunc("/logout", logout)
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-basic-routing.go
主站蜘蛛池模板: 突泉县| 石阡县| 全椒县| 镇雄县| 海门市| 涞源县| 英山县| 阿克苏市| 锡林郭勒盟| 宣威市| 华容县| 武定县| 前郭尔| 甘肃省| 秀山| 盖州市| 崇州市| 多伦县| 安平县| 黑龙江省| 鄱阳县| 信丰县| 高州市| 黄龙县| 措勤县| 日土县| 东方市| 双城市| 通榆县| 建德市| 呼伦贝尔市| 沂南县| 亳州市| 内黄县| 怀远县| 乌拉特前旗| 呈贡县| 武宁县| 视频| 大埔县| 仙居县|