当前位置:网站首页>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 查看网页,一切正常。
边栏推荐
- FL Studio 20无限试用版水果编曲下载
- [vtk] source code interpretation of vtkpolydatatoimagestencil
- Unity3D学习笔记5——创建子Mesh
- 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
- How to mix embedded MCU, arm and DSP?
- Analysis of EPS electric steering system
- Xml的(DTD,xml解析,xml建模)
- [OBS] configFile in ini format of OBS
- mysql使用update联表更新的方法
- Gut | 香港中文大学于君组揭示吸烟改变肠道菌群并促进结直肠癌(不要吸烟)
猜你喜欢

The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.

2022 northeast four provinces match VP record / supplementary questions

How should intermediate software designers prepare for the soft test

Machine learning 3.2 decision tree model learning notes (to be supplemented)

Xml的(DTD,xml解析,xml建模)

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

Kibana~Kibana的安装和配置

量化计算调研

After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good

PHP基础
随机推荐
How to make others fear you
Application of high-precision indoor positioning technology in safety management of smart factory
VS2015的下载地址和安装教程
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
R语言使用aggregate函数计算dataframe数据分组聚合的均值(sum)、不设置na.rm计算的结果、如果分组中包含缺失值NA则计算结果也为NA
STL教程8-map
Driver development based on I2C protocol
repo ~ 常用命令
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
Phpcms prompt message page Jump showmessage
The uniapp scroll view solves the problems of high adaptability and bullet frame rolling penetration.
软件测试周刊(第78期):你对未来越有信心,你对现在越有耐心。
PHP server interacts with redis with a large number of close_ Wait analysis
动态规划(区间dp)
Google Earth engine (GEE) -- when we use the front and back images to make up for the interpolation effect, what if there is no effect?
Processes and threads
ASP. Net hotel management system
(2) Base
Kubernetes 三打探针及探针方式
The excel table is transferred to word, and the table does not exceed the edge paper range