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

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
主站蜘蛛池模板: 剑河县| 高雄市| 桃源县| 罗甸县| 娄烦县| 凯里市| 巴彦县| 乳山市| 淅川县| 政和县| 康定县| 长海县| 井陉县| 吴忠市| 迁安市| 鹤山市| 逊克县| 策勒县| 太康县| 伊宁县| 平邑县| 富阳市| 汨罗市| 石楼县| 罗江县| 绩溪县| 德令哈市| 玉溪市| 台东市| 灌南县| 武强县| 临西县| 江源县| 灵武市| 阳西县| 平顺县| 武宁县| 山东| 于田县| 策勒县| 东明县|