GO怎么样搭建网站

1771
2021/2/10 8:16:00
栏目: 云计算
开发者测试专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

GO怎么样搭建网站

GO使用http.HandleFunc()方法搭建一个网站程序,具体方法如下:

1.使用http.HandleFunc()方法获取参数,并保存为 hello.go文件;

package main

import (

"io"

"log"

"net/http"

)

func helloHandler(w http.ResponseWriter, r *http.Request) {

io.WriteString(w, "Hello, world!")

}

func main() {

http.HandleFunc("/hello", helloHandler)

err := http.ListenAndServe(":8080", nil)

if err != nil {

log.Fatal("ListenAndServe: ", err.Error())

}

}

2.编译 hello.go文件;

$ go run hello.go

3.最后,在浏览器中访问http://localhost:8080/hello即可;

辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读: vscode调试go插件失败怎么解决