- Go Web Development Cookbook
- Arpit Aggarwal
- 120字
- 2021-08-27 19:01:14
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:
- 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
}
}
- Run the program with the following command:
$ go run http-server-basic-routing.go
推薦閱讀
- 計算機(jī)網(wǎng)絡(luò)與通信(第2版)
- 物聯(lián)網(wǎng)與北斗應(yīng)用
- EDA技術(shù)與VHDL編程
- 高校網(wǎng)絡(luò)道德教育研究
- 異構(gòu)基因共表達(dá)網(wǎng)絡(luò)的分析方法
- Proxmox High Availability
- 無人機(jī)通信
- 物聯(lián)網(wǎng)時代
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- 射頻通信系統(tǒng)
- Mastering JavaFX 10
- 物聯(lián)網(wǎng)技術(shù)與應(yīng)用
- Windows Server 2012 Hyper-V虛擬化管理實(shí)踐
- 從實(shí)踐中學(xué)習(xí)手機(jī)抓包與數(shù)據(jù)分析
- 設(shè)備監(jiān)控技術(shù)詳解