package main import ( "fmt" "log" "net/http" ) // w表示response對象,返回給客戶端的內容都在對象里處理 // r表示客戶端請求對象,包含了請求頭,請求參數等等 func index(w http.ResponseWriter, r *http.Request) { // 往w里寫入內容,就會在瀏覽器里輸出 fmt.Fprintf(w, "Hello golang http!") } func main() { // 設置路由,如果訪問/,則調用index方法 http.HandleFunc("/", index) // 啟動web服務,監聽9090端口 err := http.ListenAndServe(":1080", nil) if err != nil { log.Fatal("ListenAndServe: ", err) } }
在瀏覽器上輸入:http://localhost:1080/
出現如下圖:
#################################################
但是用vscode來運行程序老是出現:Code is already running!
最近使用visual studio 開發,在運行了 run code 之后,再次運行 ,總提示 Code is already running!
最終在輸出窗口 :右鍵 :stop code run
###############################################