当前位置:网站首页>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 .
边栏推荐
猜你喜欢
PHP基础
After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
The world's most popular font editor FontCreator tool
FL Studio 20 unlimited trial fruit arranger Download
解决msvcp120d.dll和msvcr120d.dll缺失
vulnhub之Ripper
Groovy测试类 和 Junit测试
vulnhub之presidential
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
2022 东北四省赛 VP记录/补题
随机推荐
POI excel cell wrap
Unity3D学习笔记5——创建子Mesh
Vulnhub's presidential
金额计算用 BigDecimal 就万无一失了?看看这五个坑吧~~
简单工厂和工厂方法模式
How to mix embedded MCU, arm and DSP?
AI模型看看视频,就学会了玩《我的世界》:砍树、造箱子、制作石镐样样不差...
vulnhub之narak
错排问题 (抽奖,发邮件)
R语言使用data.table包进行数据聚合统计计算滑动窗口统计值(Window Statistics)、计算滑动分组中位数(median)并合并生成的统计数据到原数据集中
rxjs Observable filter Operator 的实现原理介绍
R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
《剑指offer 03》数组中重复的数字
R language uses grid of gridextra package The array function combines multiple visual images of the ggplot2 package horizontally, and the ncol parameter defines the number of columns of the combined g
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
vulnhub之tomato(西红柿)
Machine learning 3.2 decision tree model learning notes (to be supplemented)
Some common terms
Xml的(DTD,xml解析,xml建模)
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”