# golang搭建静态web服务器

栏目: Go · 发布时间: 5年前

内容简介:2018年03月10日 09:58:57<article class="baidu_pl" style="box-sizing: inherit; outline: 0px; display: block; position: relative; padding-top: 16px; color: rgba(0, 0, 0, 0.75); font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "

golang搭建静态web服务器

2018年03月10日 09:58:57 晴_空 阅读数:1275

<article class="baidu_pl" style="box-sizing: inherit; outline: 0px; display: block; position: relative; padding-top: 16px; color: rgba(0, 0, 0, 0.75); font-family: -apple-system, "SF UI Text", Arial, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: common-ligatures; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_21682469/article/details/79505338

我胡汉三又回来啦。好久没发文了,为保持平台上的活跃度,我今天就分享下个刚学到的知识,使用golang搭建静态web 服务器 ,亲测可用,附代码!

使用过 golang 语言的程序猿都应该知道,在使用 golang 开发的时候,我们是不需要诸如 iis , apache , nginx , [kangle](https://www.baidu.com/s?wd=kangle&tn=24004469_oem_dg&rsv_dl=gh_pl_sl_csd) 等服务器支持的。

为什么呢?

原因是, golangnet/http 包中已经提供了 HTTP 的客户端与服务端实现方案。

网上言论都说 golang 不适合做web开发,相对 php、 java 、.net、nodejs 等各类后端语言来说,使用 golang 来做web开发,确实是一个大工程。

昨晚恰好看到一篇关于使用 golang 搭建web服务器的文章,心痒难耐,于是自己也折腾了一下,用来练练手。

我是新手上路,照搬文章里的内容,总是磕磕碰碰,每次运行都是找不到路径。代码是这样的:

func main() {
    http.Handle("/css/", http.FileServer(http.Dir("template")))
    http.Handle("/js/", http.FileServer(http.Dir("template")))

    http.ListenAndServe(":8080", nil)
}

目录结构:

src
|--main
|   |-main.go
|--template
|   |-css
|     |--admin.css
|   |-js
|     |--admin.js
|   |-html
|     |--404.html

以上运行结果是:找不到 template 这个路径。

其实我很纳闷,文章作者都可以成功运行起来这个demo,怎么到我这里,就启动不来了呢?

那么问题来了:

1.是什么原因导致程序起不来呢?
2.http.Dir()指向的是什么路径?

于是我追踪日志,如下

2018/01/07 11:09:28 open template/html/404.html: The system cannot find the path specified.

发现问题是出在 找不到路径 上。解决了第一个问题后,那么接下来就需要搞明白 http.Dir() 到底指向的是哪个路径。

我查看了官方例子:

log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))

从上面例子 http.Dir("/usr/share/doc") 可看出,该路径指向的是 linux系统 里的绝对路径。那么问题就解决了:我只需要将 http.Dir() 的路径改为运行时的相对路径,或者使用绝对路径就可以了。

另一个例子,使用http.StripPrefix()方法:

// To serve a directory on disk (/tmp) under an alternate URL
// path (/tmpfiles/), use StripPrefix to modify the request
// URL's path before the FileServer sees it:
http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp"))))

可看出, tmpfilestmp 目录下的一个子目录。

既然问题都解决了,那么就修改一下代码,重新运行

func Template_dir() string {
    template_dir := "E:\\project\\gotest\\src\\template"
    return template_dir
}

func main() {
    http.Handle("/css/", http.FileServer(http.Dir(Template_dir())))
    http.Handle("/js/", http.FileServer(http.Dir(Template_dir())))

    http.ListenAndServe(":8080", nil)
}

编译运行后,在浏览器中输入 localhost:8080/css/ ,可成功看到 template/css/ 目录下的 admin.css 文件。

</article>


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

互联网金融手册

互联网金融手册

谢平、邹传伟、刘海二 / 中国人民大学出版社 / 2014-4-1 / 68.00元

本书作者在2012年4月7日“金融四十人年会”上首次公开提出了“互联网金融”概念。在短短两年中,互联网金融已经成为中国金融界和IT界最热门的词汇之一,相关创业活动也非常活跃。本书是作者两年来深入研究、思考的结晶,畅想了金融与IT结合的未来图景,将理论与实践高度融合,与读者分享了许多深具洞察力的观点。本书力图规范互联网金融的定义,完善互联网金融的理论体系,分析互联网金融目前的六种主要类型——金融互联......一起来看看 《互联网金融手册》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具