【Go】Go语言初学
发布时间:2020-12-16 18:08:49 所属栏目:大数据 来源:网络整理
导读:Go是google新开发的语言,在并发上有着得天独厚的优势,这门语言还很适合做工具。 Go的官方网址在:https://golang.org/ 如果被墙,可以在CMD中 godoc -http=:8090 8090可以改变,数值尽量大些。 然后在浏览器中访问自己 localhost:8090 即可见Go的官方文档
Go是google新开发的语言,在并发上有着得天独厚的优势,这门语言还很适合做工具。 Go的官方网址在:https://golang.org/ 如果被墙,可以在CMD中
godoc -http=:80908090可以改变,数值尽量大些。 然后在浏览器中访问自己
localhost:8090即可见Go的官方文档。
Go语言官方提供了很多库供人们使用。借用其http库,我们可以方便将自己的本地文档共享到网络中。
// http.go package main import ( "fmt" "net/http" "os" ) func main() { port := "8080" pwd,err := os.Getwd() //获取http.exe运行的当前地址 if err != nil { fmt.Println("Error",err.Error()) return } http.Handle("/",http.FileServer(http.Dir(pwd))) fmt.Println("start http server at:",port) http.ListenAndServe(":" + port,nil) }
再来个简单例子,进入网址需要用户名和密码
// http.go package main import ( "fmt" "net/http" ) func TestHandle(res http.ResponseWriter,req *http.Request) { ac := req.FormValue("account") pw := req.FormValue("password") if ac == "mick" && pw == "123" { fmt.Println(ac) res.Write([]byte("Welcome mick!")) } else { fmt.Println(ac) res.Write([]byte("Error account or password!")) } } func main() { port := "8080" http.HandleFunc("/test",TestHandle) fmt.Println("start http server at:",port) http.ListenAndServe(":"+port,nil) } 都输入正确时:
错误时:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |