温馨提示×

GO怎么样搭建网站

九三
219
2021-02-10 08:16:00
栏目: 云计算

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即可;

0