- Go Web Development Cookbook
- Arpit Aggarwal
- 230字
- 2021-08-27 19:01:18
How it works…
Once we run the program, the HTTP server will start locally listening on port 8080. Browsing http://localhost:8080 will show us an HTML form, as shown in the following screenshot:

Let’s understand the program we have written:
- func login(w http.ResponseWriter, r *http.Request) { parsedTemplate, _ := template.ParseFiles("templates/login-form.html") parsedTemplate.Execute(w, nil) }: This is a Go function that accepts ResponseWriter and Request as input parameters, parses login-form.html, and returns a new template.
- http.HandleFunc("/", login): Here we are registering a login function with the / URL pattern using HandleFunc of the net/http package, which means the login function gets executed every time we access the HTTP URL with the / pattern passing ResponseWriter and Request as the parameters to it.
- err := http.ListenAndServe(CONN_HOST+":"+CONN_PORT, nil): Here we are calling http.ListenAndServe to serve HTTP requests that handle each incoming connection in a separate Goroutine. ListenAndServe accepts two parameters—the server address and the handler—where the server address is localhost:8080 and the handler is nil.
- if err != nil { log.Fatal("error starting http server : ", err) return}: Here we check if there is a problem with starting the server. If there is, then log the error and exit with a status code of 1.
推薦閱讀
- Cisco OSPF命令與配置手冊(cè)
- Hands-On Industrial Internet of Things
- 重新定義Spring Cloud實(shí)戰(zhàn)
- Getting Started with Grunt:The JavaScript Task Runner
- 物聯(lián)網(wǎng)之霧:基于霧計(jì)算的智能硬件快速反應(yīng)與安全控制
- Master Apache JMeter:From Load Testing to DevOps
- 網(wǎng)管第一課:網(wǎng)絡(luò)操作系統(tǒng)與配置管理
- 網(wǎng)絡(luò)綜合布線(第2版)
- 人際網(wǎng)絡(luò)
- 中國信息化年鑒2017
- 趣話通信:6G的前世、今生和未來
- Corona SDK Application Design
- ReasonML Quick Start Guide
- 無線傳感器網(wǎng)絡(luò)定位方法及應(yīng)用
- 企業(yè)網(wǎng)絡(luò)組建與維護(hù)項(xiàng)目式教程