更新 1.1
在收到有关更改处理程序路径的有用反馈后,我在我的应用程序中使用了该反馈,但奇怪的是,我仍然收到一些错误反馈。我正在尝试自己弄清楚,但我想我可以与大家分享。这也是我取消现在给出的答案的原因。还有一些答案现在让我感到困惑,无法理解这将如何为我工作。
package main
import (
"net/http" // Package http provides HTTP client and server implementations.
)
func main(){
webHost()
}
func webHost(){
// Ik heb hier later nog een mapje voor de html pagina's toegevoegd.
http.Handle("/go", http.FileServer(http.Dir("./1_Go_Files")))
http.Handle("/html", http.FileServer(http.Dir("./2_Html_Pages")))
http.Handle("/storage", http.FileServer(http.Dir("./3_Json_Storage")))
// -----------------------------------------------------------------
http.HandleFunc("/homepage", Homehandler)
http.HandleFunc("/selectiepagina", Selectiehandler)
http.HandleFunc("/userinputpagina", Userinputhandler)
http.HandleFunc("/bestelnrpagina", Bestelnrhandler)
http.HandleFunc("/statuspagina", Statushandler)
//FileServer returns a handler that serves HTTP requests with the contents of the file system rooted at root..
http.Handle("/4_Css_Styling/", http.StripPrefix("/4_Css_Styling/", http.FileServer(http.Dir("css")))) // css styling map.
http.Handle("/5_Pizza_Img/", http.StripPrefix("/5_Pizza_Img/", http.FileServer(http.Dir("img")))) // image mapje.
}
所以这里出现了新的 panic (对不起,文字墙,我不确定要遗漏什么,所以我只是复制粘贴这里的所有内容:
2018/11/12 10:07:11 http: panic serving [::1]:58084: runtime error: invalid memory address or nil pointer dereference
goroutine 4 [running]:
net/http.(*conn).serve.func1(0xc0000de000)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001160e0, 0x71ac40, 0x7f5300, 0xc000106120, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc0001160e0, 0xc000124000)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001160e0, 0xc000124000)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001160e0, 0xc000124000)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001160e0, 0xc000124000)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc0000de000, 0x7fa120, 0xc0000320c0)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:11 http: panic serving [::1]:58087: runtime error: invalid memory address or nil pointer dereference
goroutine 19 [running]:
net/http.(*conn).serve.func1(0xc00004cf00)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001161c0, 0x71ac40, 0x7f5300, 0xc0001061e0, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc0001161c0, 0xc000126000)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001161c0, 0xc000126000)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001161c0, 0xc000126000)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001161c0, 0xc000126000)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00004cf00, 0x7fa120, 0xc00004e240)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:11 http: panic serving [::1]:58088: runtime error: invalid memory address or nil pointer dereference
goroutine 37 [running]:
net/http.(*conn).serve.func1(0xc000172000)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001162a0, 0x71ac40, 0x7f5300, 0xc000106360, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc0001162a0, 0xc000124100)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001162a0, 0xc000124100)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001162a0, 0xc000124100)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001162a0, 0xc000124100)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc000172000, 0x7fa120, 0xc000110240)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:11 http: panic serving [::1]:58089: runtime error: invalid memory address or nil pointer dereference
goroutine 39 [running]:
net/http.(*conn).serve.func1(0xc0001720a0)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc000116380, 0x71ac40, 0x7f5300, 0xc0001064e0, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc000116380, 0xc000124200)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc000116380, 0xc000124200)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc000116380, 0xc000124200)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc000116380, 0xc000124200)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc0001720a0, 0x7fa120, 0xc000110340)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:12 http: panic serving [::1]:58090: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc00004cfa0)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc000116460, 0x71ac40, 0x7f5300, 0xc000106660, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc000116460, 0xc000126100)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc000116460, 0xc000126100)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc000116460, 0xc000126100)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc000116460, 0xc000126100)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00004cfa0, 0x7fa120, 0xc00004e2c0)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:12 http: panic serving [::1]:58091: runtime error: invalid memory address or nil pointer dereference
goroutine 41 [running]:
net/http.(*conn).serve.func1(0xc000172140)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001cc000, 0x71ac40, 0x7f5300, 0xc000067290, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc0001cc000, 0xc000124300)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001cc000, 0xc000124300)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001cc000, 0xc000124300)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001cc000, 0xc000124300)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc000172140, 0x7fa120, 0xc000110440)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
2018/11/12 10:07:12 http: panic serving [::1]:58092: runtime error: invalid memory address or nil pointer dereference
goroutine 22 [running]:
net/http.(*conn).serve.func1(0xc00004d040)
C:/tools/go/src/net/http/server.go:1746 +0xd7
panic(0x73a9c0, 0xa34b30)
C:/tools/go/src/runtime/panic.go:513 +0x1c7
html/template.(*Template).escape(0x0, 0x0, 0x0)
C:/tools/go/src/html/template/template.go:95 +0x35
html/template.(*Template).Execute(0x0, 0x7f7580, 0xc0001cc0e0, 0x71ac40, 0x7f5300, 0xc000067410, 0x3)
C:/tools/go/src/html/template/template.go:119 +0x36
main.Homehandler(0x7f9f60, 0xc0001cc0e0, 0xc000126200)
C:/Users/Taavetti/Documents/Go/Sources/src/Projects/VillaBrutus_Main/1_Go_Files/0_Homepagina.go:14 +0xc8
net/http.HandlerFunc.ServeHTTP(0x7b3ca8, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
C:/tools/go/src/net/http/server.go:1964 +0x4b
net/http.(*ServeMux).ServeHTTP(0xa438e0, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
C:/tools/go/src/net/http/server.go:2361 +0x12e
net/http.serverHandler.ServeHTTP(0xc00004b040, 0x7f9f60, 0xc0001cc0e0, 0xc000126200)
C:/tools/go/src/net/http/server.go:2741 +0xb2
net/http.(*conn).serve(0xc00004d040, 0x7fa120, 0xc00004e3c0)
C:/tools/go/src/net/http/server.go:1847 +0x64d
created by net/http.(*Server).Serve
C:/tools/go/src/net/http/server.go:2851 +0x2fc
同样,我仍在尝试自己解决这个问题,因为我想表明我也在为此付出努力,而不仅仅是试图从您那里得到所有答案。但如果你能帮助我朝着正确的方向前进,那将会很有帮助。
(同样,我是新用户,如果我违反了此线程中的某些规则,请在您否决此威胁后通知我,我真的很喜欢 stackoverflow,我正在尝试了解这个社区的运作方式。)
(添加了我当前的应用程序,包括错误,也许这有帮助?,请注意有一些荷兰语评论)
最佳答案
您已为 / 分配了多个处理程序。
看你的代码两次:
func webHost(){
http.Handle("/",
http.FileServer(http.Dir("./html_paginas")))
http.Handle("/", http.FileServer(http.Dir("./go_files")))
http.Handle("/", http.FileServer(http.Dir("./storage")))
//...
}
所以 http 必须在这种情况下 panic 。因此,对此的修复是:
func webHost(){
http.Handle("/html",
http.FileServer(http.Dir("./html_paginas")))
http.Handle("/go", http.FileServer(http.Dir("./go_files")))
http.Handle("/storage",
http.FileServer(http.Dir("./storage")))
//...
}
或者:
func webHost(){
http.Handle("/", http.FileServer(http.Dir("./root")))
//...
}
现在将您的 html,go,存储目录移动到 root 目录。
关于json - 更新 1.1 在文件夹中运行 Go 文件和 JSON 文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53223002/
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta