简单web

简单web

image-20211108085431850

一、hello web

package main

import (
	"fmt"
	"net/http"
)

/*
@author RandySun
@create 2021-11-08-22:42
*/

func sayHello(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprintln(w, "hello web")

}
func main() {
	http.HandleFunc("/hello", sayHello)
	err := http.ListenAndServe(":9999", nil)
	if err != nil {
		fmt.Printf("http server failed err:%#v", err)
	}
	
}

image-20211108224950141

二、插入html

package main

import (
	"fmt"
	"net/http"
)

/*
@author RandySun
@create 2021-11-08-22:42
*/

func sayHello(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprintln(w, "<H1>hello web</H1>")
	_, _ = fmt.Fprintln(w, "<H1 style='color: red'>hello web</H1>")

}
func main() {
	http.HandleFunc("/hello", sayHello)
	err := http.ListenAndServe(":9999", nil)
	if err != nil {
		fmt.Printf("http server failed err:%#v", err)
	}

}

image-20211108225804450

三、读取文件渲染

// helloWeb.txt

<h1 style="color:red">hello web</h1>
<h2>hello web</h2>
<img id="randy" src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F1114%2F0512210S939%2F2105120S939-12-1200.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638976149&t=ab8df8adcb0ce26f03170766e7744d94">

<button id="sun">点我</button>
<script>
   document.getElementById("sun").onclick=function(){
        document.getElementById("randy").src="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fup.enterdesk.com%2Fedpic%2F12%2F1f%2Fa0%2F121fa0f4730a034237c13b7595db3a16.jpg&refer=http%3A%2F%2Fup.enterdesk.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1638976181&t=125bfc28da1fb97e6fe5e179fdead261"
   }
</script>

package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
)

/*
@author RandySun
@create 2021-11-08-22:42
*/

func sayHello(w http.ResponseWriter, r *http.Request) {
	_, _ = fmt.Fprintln(w, "<H1>hello web</H1>")
	_, _ = fmt.Fprintln(w, "<H1 style='color: red'>hello web</H1>")

}

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

	b, _ := ioutil.ReadFile("G:\\goproject\\go\\bubble\\demo\\01httpTest\\helloWeb.txt")
	a, _ := fmt.Fprintln(w, string(b))
	fmt.Println(a)

}
func main() {
	http.HandleFunc("/hello", sayHello)
	http.HandleFunc("/helloFile", sayHelloFile)
	err := http.ListenAndServe(":9999", nil)
	if err != nil {
		fmt.Printf("http server failed err:%#v", err)
	}

}

image-20211108231235255

image-20211108213848772

posted @ 2021-11-29 22:43  RandySun  阅读(99)  评论(0编辑  收藏  举报