- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
目录树:
.
├── main.go
└── web
├── app.go
└── views
├── index.html
└── js
└── app.jsx
这个有效:
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./web/views")))
http.ListenAndServe(":3000", nil)
}
但这会返回 404 页面未找到
:
main.go:
package main
import (
"{dir with main.go}/web"
)
func main() {
web.StartHttp()
}
应用程序:
package web
import (
"fmt"
"net/http"
)
func StartHttp() {
fmt.Println("STARTHTTP - CHECK 01")
http.Handle("/", http.FileServer(http.Dir("./views")))
http.ListenAndServe(":3000", nil)
}
终端打印STARTHTTP - CHECK 01
,所以调用了StartHttp()
函数,终端要求允许传入网络连接,所以http服务器似乎在端口上监听。
是否有某种类型的上下文未传递给其他包?
最佳答案
请记住,Go 是一种编译型语言;程序所做的大部分事情都发生在运行时。
特别是,在这种情况下,对 http.Dir()
的调用发生在运行时,这意味着路径是在运行时评估的。
因为您提供的路径是相对的,所以它是相对于您运行应用程序的工作目录的。源代码所在目录与此处无关。
在对 http.Dir()
的一次调用中,您提供参数 ./web/views
,但在另一次调用中,您提供参数 ./意见
。事实证明,基于您执行程序的目录的正确路径是 ./web/views
。当您使用错误的路径执行程序时,您会收到 404 page not found
错误,因为指定的路径在您的工作目录中不存在。
关于go - "http.FileServer(http.Dir...))"不能在单独的包中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52141282/
这个问题在这里已经有了答案: What's the difference between html[lang="en"] and html:lang(en) in CSS? (4 个答案) 关闭 6
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
来自 ./configure --help: --with-zlib=DIR Include ZLIB support (requires zlib >= 1.0.9) --with-zlib-
我正在尝试检查我的 pod 规范并收到以下错误。 [!]“Classes”存储库中的“Classes”Pod 遇到意外版本目录“ios”。 我的源文件夹设置如下: 类 - iOS - 操作系统 Pod
在 Python 中有一个名为 dir 的内置函数。这用于获取对象所有属性的列表。 我了解它的作用,但我对为什么将其称为 dir 感到困惑。这个名称与从对象中获取属性有什么关系? 最佳答案 IIRC
我正在使用以下方法遍历文件夹: Dim file As Variant file = Dir(myFolder) While (file <> "" ) mySub file '
在 Python 的交互式 shell 中,执行 dir() 会打印一个名称列表。但是在脚本中 dir() 不直接提供任何输出,我必须使用 print(dir())。 不仅仅是dir(),其他内置函数
我明白它的作用,并且一直在使用它。但有趣的是,我还没有找到说明“DIR”的实际含义 的文档。 “目录”似乎不对。 这不是“console.log() 和 console.dir() 之间有什么区别?”
我正在尝试使用字符向量在我的工作目录中创建一系列目录。没有递归,只是迭代。没有什么花哨。例如,我可以很容易地做到这一点: lapply(state.name, dir.create) 它在我的工作目录
dir(x) 和 dir(x.__class__) 有什么区别?后者返回不同的属性列表,但与前者重叠。 例如,SQLAlchemy 的 sqlalchemy.create_engine() 函数创建一
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我正在尝试使用 Dir[] 和/或使用 Dir.foreach 对目录进行 glob: files = Dir["#{options[:dir]}/**/*"].reject { |file| Fil
Git 存储库中.gitignore 文件中的/dir 和/dir/ 有区别吗? 以下有何不同? /dir /dir/ /dir/* 最佳答案 是的,这些规则是不同的。 /dir 将匹配文件、目录、链
我正在尝试编译Bootstrap 5.3以及定制的sass文件。。这就是我的东西。奇怪的是,最后一节生成了下面的css。我预计会看到下面的css。它在开头添加了[dir]。为什么要添加[dir]?我怎
我正在尝试编译Bootstrap 5.3以及定制的sass文件。。这就是我的东西。奇怪的是,最后一节生成了下面的css。我预计会看到下面的css。它在开头添加了[dir]。为什么要添加[dir]?我怎
我正在尝试编译Bootstrap 5.3以及定制的sass文件。。这就是我的东西。奇怪的是,最后一节生成了下面的css。我预计会看到下面的css。它在开头添加了[dir]。为什么要添加[dir]?我怎
你可以找到各种文档来指定 XML 文档内容的语言,使用 xml:lang,如下所示: ... HTML 和 XHTML 具有 dir 属性,可让您指定文本在默认情况下应被视为从左到右,还是默认从右到左
当我 echo 时,输出似乎相同编辑它。 我还测试了其他命令,例如 open ,但两者的结果是一样的。 最佳答案 在传统 sh -style 模式匹配,*匹配文件名中的零个或多个字符,因此 * 之间没
我想在不同时间将一个目录复制到另一个目录中。每次,我都希望新名称比以前高一个数字/我希望能够获取该数字并将其存储到变量中。 所以如果我有一个: mymaindir /home/user/dirs/ST
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我是一名优秀的程序员,十分优秀!