go language go语言

go语言实现简单的WEB服务器

TheHuPost newsletter上线了, 欢迎大家订阅! 
对C++, Java,JavaScript,Python等编程语言有兴趣的,也可以看看编程语言. 
也欢迎大家来论坛与我讨论

最近稍微了解了下Go语言, 简单实现了一个Hello World 的web 服务器

// hello
package main

import (
	"fmt"
	"log"
	"net/http"
	"strings"
)

func sayHelloName(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	fmt.Println(r.Form)
	fmt.Println("path: ", r.URL.Path)
	fmt.Println("scheme: ", r.URL.Scheme)
	fmt.Println(r.Form["url_log"])

	for k, v := range r.Form {
		fmt.Println("key: ", k)
		fmt.Println("val: ", strings.Join(v, ""))

	}

	fmt.Fprintf(w, "Hello World")

}

func main() {
	http.HandleFunc("/", sayHelloName)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		log.Fatal("ListenAndServer: ", err)
	}

}

编译以上代码, 然后运行.exe 文件,启动WEB 服务器

在本地浏览器上访问: http://localhost:9090  ,页面将会显示Hello World

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top