当前位置:网站首页>Go语言实现静态服务器
Go语言实现静态服务器
2022-07-03 11:01:00 【星哥玩云】
学习Go语言的一些感受,不一定准确。 假如发生战争,JAVA一般都是充当航母战斗群的角色。 一旦出动,就是护卫舰、巡洋舰、航母舰载机、预警机、电子战飞机、潜艇等等 浩浩荡荡,杀将过去。 (JVM,数十个JAR包,Tomcat中间件,SSH框架,各种配置文件...天生就是重量级的,专为大规模作战) 而GO语言更像F35战斗轰炸机 单枪匹马,悄无声息,投下炸弹然后走人。 专属轰炸机,空战也会一点点. 实在搞不定,就叫它大哥F22。 (GO是编译型语言,不需要依赖,不需要虚拟机,可以调用C代码并且它足够简单,却非常全面) 计划Go语言学习的知识点 1.搭建Http服务 2.连接数据库 3.本地IO 4.多线程 5.网络 6.调用本地命令 7.调用C语言代码 首先,搭建一个静态的服务器 我写程序喜欢使用HTML通过AJAX发送JSON请求到后端处理。 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)
}
} 网上看到一个更BT的方法..
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("/tmp/static/")))
http.ListenAndServe(":8080", nil)
} 将EasyUI前端框架解压到 /tmp/static 目录下
在GOPATH下执行 go run HttpServer.go --path=/tmp/static 查看网页,一切正常。
边栏推荐
- R语言使用gridExtra包的grid.arrange函数将ggplot2包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
- 剑指offer专项32-96题做题笔记
- 简单工厂和工厂方法模式
- Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
- VPP three-layer network interconnection configuration
- R language uses data The table package performs data aggregation statistics, calculates window statistics, calculates the median of sliding groups, and merges the generated statistical data into the o
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- Spl06-007 air pressure sensor (example of barometer)
- typeScript
- Phpcms prompt message page Jump showmessage
猜你喜欢
STL教程9-容器元素深拷贝和浅拷贝问题
MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
Leetcode 46: full arrangement
MCDF实验1
Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable
Numpy np. Max and np Maximum implements the relu function
STL教程10-容器共性和使用场景
Machine learning 3.2 decision tree model learning notes (to be supplemented)
(2) Base
Event preview | the live broadcast industry "rolled in" to drive new data growth points with product power
随机推荐
Hongmeng fourth training
Processes and threads
MySQL union和union all区别
ORACLE进阶(一) 通过EXPDP IMPDP命令实现导dmp
(2) Base
Machine learning 3.2 decision tree model learning notes (to be supplemented)
Matlab extracts numerical data from irregular txt files (simple and practical)
The excel table is transferred to word, and the table does not exceed the edge paper range
Oracle withdraw permission & create role
C language two-dimensional array
Cadence background color setting
previous permutation lintcode51
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
The world's most popular font editor FontCreator tool
MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)
动态规划(区间dp)
PHP server interacts with redis with a large number of close_ Wait analysis
Excel表格转到Word中,表格不超边缘纸张范围
聊聊Flink框架中的状态管理机制
typeScript