当前位置:网站首页>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 查看网页,一切正常。
边栏推荐
- (数据库提权——Redis)Redis未授权访问漏洞总结
- Technical experts from large factories: how can engineers improve their communication skills?
- Internet socket (non) blocking write/read n bytes
- P3250 [HNOI2016] 网络 + [NECPC2022] F.Tree Path 树剖+线段树维护堆
- 一文搞懂Go语言Context
- C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
- Kibana~Kibana的安装和配置
- PHP Basics
- uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
猜你喜欢

STL教程9-容器元素深拷贝和浅拷贝问题

Kubernetes 三打探针及探针方式

Spl06-007 air pressure sensor (example of barometer)

MATLAB extrait les données numériques d'un fichier txt irrégulier (simple et pratique)

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

uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。

FL Studio 20无限试用版水果编曲下载

Driver development based on I2C protocol

Incremental database backup - DB incr DB full

ASP.NET-酒店管理系統
随机推荐
同事写了一个责任链模式,bug无数...
Cadence background color setting
[OBS] encapsulate the basic process of OBS acquisition
R语言使用gridExtra包的grid.arrange函数将lattice包的多个可视化图像横向组合起来,ncol参数自定义组合图列数、nrow参数自定义组合图行数
动态规划(区间dp)
Intel 13th generation core flagship exposure, single core 5.5ghz
The world's most popular font editor FontCreator tool
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
JGG专刊征稿:时空组学
活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
Matlab extracts numerical data from irregular txt files (simple and practical)
How PHP solves the problem of high concurrency
Software testing weekly (issue 78): the more confident you are about the future, the more patient you are about the present.
导师对帮助研究生顺利完成学业提出了20条劝告:第一,不要有度假休息的打算.....
AOSP ~ NTP ( 网络时间协议 )
How to become a senior digital IC Design Engineer (1-3) Verilog coding syntax: Verilog behavior level, register transfer level, gate level (abstract level)
简单工厂和工厂方法模式
phpcms 提示信息頁面跳轉showmessage
How to get started embedded future development direction of embedded
How to become a senior digital IC Design Engineer (1-2) Verilog coding syntax: Verilog 1995, 2001, 2005 standards