1. 程式人生 > >Go語言進行web開發(一) 搭建一個簡單的web伺服器

Go語言進行web開發(一) 搭建一個簡單的web伺服器

Go語言也被成為“自帶電池”的語言,有大量的web相關工具整合在其中,構建web應用成了一件信手拈來的事情,只要呼叫http包的兩個函式就可以了。

package main

import (
    "fmt"
    "net/http"
    "log"
)

func responseHello(w http.ResponseWriter, r *http.Request){
    //將字串傳送給客戶端
    fmt.Fprintf(w, "Hello Golang!")
}

func main(){
    //設定要解析的URL路由
    http.HandleFunc("/"
, responseHello) //設定監聽的埠,開始監聽 errInfo := http.ListenAndServe(":8080", nil) if errInfo != nil { log.Fatal("ListenAndServe: ", errInfo) } }