当前位置:网站首页>Go language to realize static server
Go language to realize static server
2022-07-03 11:50:00 【Brother Xing plays with the clouds】
Study Go Language Some feelings of , Not necessarily accurate . In case of war ,JAVA It usually acts as an aircraft carrier battle group . Once out , It's the frigate 、 cruiser 、 Carrier based aircraft 、 Awacs... 、 EW aircraft 、 Submarines and so on The majestic , Kill will pass . (JVM, Dozens of JAR package ,Tomcat middleware ,SSH frame , Various configuration files ... Born to be a heavyweight , Designed for large-scale operations ) and GO Language is more like F35 Fighter bomber single-handed , quietly , Drop the bomb and leave . Exclusive bomber , Air combat will also be a little . I'm not sure , Just call it big brother F22. (GO It's a compiled language , Don't rely on , No virtual machine required , You can call C Code and it's simple enough , But very comprehensive ) plan Go Language What to learn 1. build Http service 2. Connect to database 3. Local IO 4. Multithreading 5. The Internet 6. Call the local command 7. call C The language code First , Build a static The server I like to write programs using HTML adopt AJAX send out JSON Request to back-end processing . HttpServer.go package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var realPath *string
func staticResource(w http.ResponseWriter, r *http.Request) {
path := r.URL.Path
request_type := path[strings.LastIndex(path, "."):]
switch request_type {
case ".css":
w.Header().Set("content-type", "text/css")
case ".js":
w.Header().Set("content-type", "text/javascript")
default:
}
fin, err := os.Open(*realPath + path)
defer fin.Close()
if err != nil {
log.Fatal("static resource:", err)
}
fd, _ := ioutil.ReadAll(fin)
w.Write(fd)
}
func main() {
realPath = flag.String("path", "", "static resource path")
flag.Parse()
http.HandleFunc("/", staticResource)
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe:", err)
}
} See a more BT Methods ..
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("/tmp/static/")))
http.ListenAndServe(":8080", nil)
} take EasyUI Unzip the front-end framework to /tmp/static Under the table of contents
stay GOPATH perform go run HttpServer.go --path=/tmp/static View the web page , Everything is all right .
such Go Language In less than 50 Line code , Less than after compilation 7M The executable of , A simple Static servers .
边栏推荐
- Dynamic programming (interval DP)
- ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
- R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- phpcms 提示信息頁面跳轉showmessage
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
- Keepalived中Master和Backup角色选举策略
- 牛牛的组队竞赛
- Unity3D学习笔记5——创建子Mesh
- Using onvif protocol to operate the device
- Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation
猜你喜欢

Vulnhub geminiinc V2

Xiaopeng P7 hit the guardrail and the airbag did not pop up. The official responded that the impact strength did not meet the ejection requirements

Web security summary

Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)

AOSP ~ NTP ( 网络时间协议 )

银泰百货点燃城市“夜经济”

The world's most popular font editor FontCreator tool

VS2015的下载地址和安装教程

Web安全总结

rxjs Observable filter Operator 的实现原理介绍
随机推荐
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
剑指offer专项32-96题做题笔记
How to mix embedded MCU, arm and DSP?
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
P3250 [hnoi2016] Network + [necpc2022] f.tree path tree section + segment tree maintenance heap
Go语言实现静态服务器
ASP. Net hotel management system
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
Groovy测试类 和 Junit测试
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
How to make others fear you
Unity3D学习笔记5——创建子Mesh
STL tutorial 10 container commonalities and usage scenarios
Asyncio warning deprecationwarning: there is no current event loop
STL tutorial 8-map
rxjs Observable filter Operator 的实现原理介绍
抓包整理外篇fiddler———— 会话栏与过滤器[二]
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
2022年湖南工学院ACM集训第二次周测题解
vulnhub之Ripper