- 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
推薦閱讀
- EJB 3.1從入門到精通
- Cisco OSPF命令與配置手冊
- RCNP實驗指南:構建高級的路由互聯網絡(BARI)
- 物聯網智慧安監(jiān)技術
- 光網絡評估及案例分析
- Spring Boot 2.0 Projects
- Hands-On Chatbot Development with Alexa Skills and Amazon Lex
- Proxmox High Availability
- Windows Server 2003 Active Directory Design and Implementation: Creating, Migrating, and Merging Networks
- 智慧城市中的移動互聯網技術
- 圖解手機元器件維修技巧
- Mastering Dart
- 2小時讀懂物聯網
- 通信十年:擁抱互聯網
- 語音信號處理及Blackfin DSP實現