- 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.
推薦閱讀
- 解析QUIC/HTTP3:未來互聯(lián)網(wǎng)的基石
- 信息通信網(wǎng)絡(luò)建設(shè)安全管理概要2
- Building RESTful Web Services with Spring 5(Second Edition)
- 互聯(lián)網(wǎng)安全的40個(gè)智慧洞見:2014年中國互聯(lián)網(wǎng)安全大會(huì)文集
- 網(wǎng)絡(luò)的琴弦:玩轉(zhuǎn)IP看監(jiān)控
- 世界互聯(lián)網(wǎng)發(fā)展報(bào)告·2019
- 端到端QoS網(wǎng)絡(luò)設(shè)計(jì)
- 紅藍(lán)攻防:構(gòu)建實(shí)戰(zhàn)化網(wǎng)絡(luò)安全防御體系
- 5G+區(qū)塊鏈
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- NB-IoT原理和優(yōu)化
- React Design Patterns and Best Practices(Second Edition)
- 從物聯(lián)到萬聯(lián):Node.js與樹莓派萬維物聯(lián)網(wǎng)構(gòu)建實(shí)戰(zhàn)
- “IPv6+”網(wǎng)絡(luò)技術(shù)創(chuàng)新:構(gòu)筑數(shù)字經(jīng)濟(jì)發(fā)展基石
- 窄帶物聯(lián)網(wǎng)(NB-IoT)標(biāo)準(zhǔn)與關(guān)鍵技術(shù)