- 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
推薦閱讀
- SEO 20日
- GPS/GNSS原理與應用(第3版)
- Spring Boot 2.0 Projects
- 5G承載網網絡規(guī)劃與組網設計
- JBoss EAP6 High Availability
- 互聯網基礎資源技術與應用發(fā)展態(tài)勢(2021—2023)
- 網絡安全技術與解決方案(修訂版)
- SSL VPN : Understanding, evaluating and planning secure, web/based remote access
- CCNP TSHOOT(642-832)認證考試指南
- 基于性能的保障理論與方法
- Learning Storm
- 深入理解OpenStack Neutron
- 一本書讀懂TCP/IP
- Building RESTful Web Services with .NET Core
- Hands-On Cloud:Native Microservices with Jakarta EE