当前位置:网站首页>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 .
边栏推荐
- vulnhub之narak
- P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
- Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
- The R language uses the hist function in the native package (basic import package, graphics) to visualize the histogram plot
- 鸿蒙第三次培训(项目实训)
- Vulnhub geminiinc V2
- R语言使用原生包(基础导入包、graphics)中的hist函数可视化直方图(histogram plot)
- CSRF
- How to get started embedded future development direction of embedded
- MySQL searches and sorts out common methods according to time
猜你喜欢

Kibana~Kibana的安装和配置
![抓包整理外篇fiddler———— 会话栏与过滤器[二]](/img/04/e9cc027d753e7049f273d866eefdce.png)
抓包整理外篇fiddler———— 会话栏与过滤器[二]

外插散点数据

The excel table is transferred to word, and the table does not exceed the edge paper range

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

VS2015的下载地址和安装教程

软考中级软件设计师该怎么备考

机器学习 3.2 决策树模型 学习笔记(待补)

Kibana - installation and configuration of kibana

Hongmeng third training (project training)
随机推荐
ASP. Net hotel management system
机器学习 3.2 决策树模型 学习笔记(待补)
STL tutorial 8-map
一些常用术语
GCC compilation process and dynamic link library and static link library
Viewing binary bin files with notepad++ editor
STL tutorial 10 container commonalities and usage scenarios
软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
How PHP solves the problem of high concurrency
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
ArcGIS应用(二十一)Arcmap删除图层指定要素的方法
STL教程10-容器共性和使用场景
R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
vulnhub之cereal
Gut | Yu Jun group of the Chinese University of Hong Kong revealed that smoking changes intestinal flora and promotes colorectal cancer (do not smoke)
STL Tutorial 9 deep copy and shallow copy of container elements
Vulnhub geminiinc
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
vulnhub之GeminiInc