Golang is so easy to create http-server, just use package “net/http”.
ListeAndServe start listen a port, if client have request, HandleFunc registry path and point to a function
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func foo(w http.ResponseWriter, r *http.Request) {
file, err := ioutil.ReadFile("./test.html")
if err != nil {
w.Write([]byte(fmt.Sprintf("%v", err)))
}
w.Write(file)
}
func main() {
http.HandleFunc("/foo", foo)
http.ListenAndServe("0.0.0.0:9090", nil)
}
We can wirte html code in test.html
<h1 style="color:red">bar</h1>
<a href="https://www.baidu.com">点我去百度</a>
<img id="i1" src="https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1602760046007&di=620d28f5f0592db0fd175c3f923f27ab&imgtype=0&src=http%3A%2F%2Fimg1.cache.netease.com%2Fcatchpic%2FE%2FE5%2FE5DD0A8099E2D28226465C6894F7A7A1.jpg">
<button id="b1">点我</button>
<script>
document.getElementById("b1").onclick=function () {
document.getElementById("i1").src="https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3035184011,4060465026&fm=26&gp=0.jpg"
}
</script>