当前位置:网站首页>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 查看网页,一切正常。
边栏推荐
- MySQL union和union all区别
- After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
- AOSP ~ NTP ( 网络时间协议 )
- Programmers' entrepreneurial trap: taking private jobs
- Hongmeng third training (project training)
- Incremental database backup - DB incr DB full
- Understand go language context in one article
- Double linked list of linear list
- C language log base zlog basic use
- 软考中级软件设计师该怎么备考
猜你喜欢

Hongmeng fourth training

2022 northeast four provinces match VP record / supplementary questions

Web security summary

Cuiyusong, CTO of youzan: the core goal of Jarvis is to make products smarter and more reliable

The world's most popular font editor FontCreator tool

This article explains the complex relationship between MCU, arm, MCU, DSP, FPGA and embedded system

Abandon the Internet after 00: don't want to enter a big factory after graduation, but go to the most fashionable Web3

Use typora to draw flow chart, sequence diagram, sequence diagram, Gantt chart, etc. for detailed explanation

Numpy np. Max and np Maximum implements the relu function

Incremental database backup - DB incr DB full
随机推荐
Yintai department store ignites the city's "night economy"
phpcms 提示信息页面跳转showmessage
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据、在折线移动方向添加数据点
After setting up ADG, instance 2 cannot start ora-29760: instance_ number parameter not specified
How to clean up v$rman_ backup_ job_ Details view reports error ora-02030
(数据库提权——Redis)Redis未授权访问漏洞总结
Key switch: press FN when pressing F1-F12
DS90UB949
Excel表格转到Word中,表格不超边缘纸张范围
并发编程-单例
Oracle withdraw permission & create role
Encapsulate a koa distributed locking middleware to solve the problem of idempotent or repeated requests
银泰百货点燃城市“夜经济”
Numpy np.max和np.maximum实现relu函数
解决msvcp120d.dll和msvcr120d.dll缺失
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
. \vmware-vdiskmanager. exe -k “c:\\xxxxx.vmdk”
[OBS] configFile in ini format of OBS
Arctangent entropy: the latest SCI paper in July 2022
VS2015的下载地址和安装教程