17. Go极简教程 Web服务器

2018-06-03  本文已影响56人  超级大柱子

使用http启动一个服务

package main

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

func hello(w http.ResponseWriter, r *http.Request) {
    io.WriteString(w, "hello world")
}

func main() {
    http.HandleFunc("/", hello)
    log.Println("listen: http://localhost:8000/")
    http.ListenAndServe(":8000", nil)
}

访问 http://localhost:7000 可以看到来自程序的问候

实际工作中,我们会使用Iris或者beego编写一个Web服务

参考资料:
http://go-tour-zh.appspot.com/
https://studygolang.com/articles/7854

Go极简教程 继续阅读( 目录)

上一篇 下一篇

猜你喜欢

热点阅读