- Go Web Development Cookbook
- Arpit Aggarwal
- 107字
- 2021-08-27 19:01:10
How to do it…
In this recipe, we are going to create a simple HTTP server that will render Hello World! when we browse http://localhost:8080 or execute curl http://localhost:8080 from the command line. Perform the following steps:
- Create http-server.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 main()
{
http.HandleFunc("/", helloWorld)
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.go
推薦閱讀
- 智慧城市:大數(shù)據(jù)、互聯(lián)網(wǎng)時(shí)代的城市治理(第4版)
- RCNP實(shí)驗(yàn)指南:構(gòu)建高級(jí)的路由互聯(lián)網(wǎng)絡(luò)(BARI)
- 面向物聯(lián)網(wǎng)的CC2530與傳感器應(yīng)用開(kāi)發(fā)
- TCP/IP入門(mén)經(jīng)典(第5版)
- 雷達(dá)饋線技術(shù)
- 數(shù)字調(diào)制解調(diào)技術(shù)的MATLAB與FPGA實(shí)現(xiàn):Altera/Verilog版(第2版)
- Building Web Applications with ArcGIS
- Practical Web Penetration Testing
- 數(shù)據(jù)血緣分析原理與實(shí)踐
- Hands-On Bitcoin Programming with Python
- Building RESTful Web Services with .NET Core
- 新媒體交互藝術(shù)
- 新IP:面向泛在全場(chǎng)景的未來(lái)數(shù)據(jù)網(wǎng)絡(luò)
- ElasticSearch Server
- SD-WAN 架構(gòu)與技術(shù)