是什么导致我的HTTP服务器出现故障;退出状态-1073741819;?
作为练习,我创建了一个生成随机游戏机制的小型HTTP服务器,类似于。我在Windows7(32位)系统上编写了它,它工作得完美无缺。但是,当我在家用计算机Windows 7(64位)上运行它时,它总是会失败,并显示相同的消息:是什么导致我的HTTP服务器出现故障;退出状态-1073741819;?,http,windows-7,go,windows-7-x64,Http,Windows 7,Go,Windows 7 X64,作为练习,我创建了一个生成随机游戏机制的小型HTTP服务器,类似于。我在Windows7(32位)系统上编写了它,它工作得完美无缺。但是,当我在家用计算机Windows 7(64位)上运行它时,它总是会失败,并显示相同的消息:退出状态-1073741819。我还没有在网上找到任何引用状态代码的东西,所以我不知道它有多重要 以下是服务器的代码,并对冗余进行了删节: package main import ( "fmt" "math/rand" "time" "ne
退出状态-1073741819
。我还没有在网上找到任何引用状态代码的东西,所以我不知道它有多重要
以下是服务器的代码,并对冗余进行了删节:
package main
import (
"fmt"
"math/rand"
"time"
"net/http"
"html/template"
)
// Info about a game mechanic
type MechanicInfo struct { Name, Desc string }
// Print a mechanic as a string
func (m MechanicInfo) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Desc)
}
// A possible game mechanic
var (
UnkillableObjects = &MechanicInfo{"Avoiding Unkillable Objects",
"There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."}
//...
Race = &MechanicInfo{"Race",
"The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."}
)
// Slice containing all game mechanics
var GameMechanics []*MechanicInfo
// Pseudorandom number generator
var prng *rand.Rand
// Get a random mechanic
func RandMechanic() *MechanicInfo {
i := prng.Intn(len(GameMechanics))
return GameMechanics[i]
}
// Initialize the package
func init() {
prng = rand.New(rand.NewSource(time.Now().Unix()))
GameMechanics = make([]*MechanicInfo, 34)
GameMechanics[0] = UnkillableObjects
//...
GameMechanics[33] = Race
}
// serving
var index = template.Must(template.ParseFiles(
"templates/_base.html",
"templates/index.html",
))
func randMechHandler(w http.ResponseWriter, req *http.Request) {
mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
if err := index.Execute(w, mechanics); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", randMechHandler)
if err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}
}
此外,和
是什么导致了这个问题?是否有一个进程可以调试这样一个神秘的退出状态?当我运行它时,我发现以下两个错误:
template: content:6: nil pointer evaluating *main.MechanicInfo.Name
http: multiple response.WriteHeader calls
前者在web浏览器中,后者在我启动服务器的控制台窗口中
零指针问题是因为您的简化程序将GameMechanics[1:32]设置为零
第二个错误很有趣。程序中唯一调用http.ResponseWriter上任何方法的地方是index.Execute内部,这不是您的代码——这意味着html/模板中可能发生了错误。我正在用Go 1.0.2测试这个
我将_base.html放在index.html的顶部,然后将索引更改为:
var index = template.Must(template.ParseFiles("templates/index.html"))
http.WriteHeaders警告消失了
这不是一个真正的答案,而是一个你可以探索的方向
作为奖励,这里有一种编写程序的更为“可行的方法”。请注意,我简化了PRNG的使用(您不需要实例化,除非您希望多个PRNG并行),并简化了结构初始值设定项:
package main
import (
"fmt"
"html/template"
"math/rand"
"net/http"
)
// Info about a game mechanic
type MechanicInfo struct{ Name, Desc string }
// Print a mechanic as a string
func (m MechanicInfo) String() string {
return fmt.Sprintf("%s: %s", m.Name, m.Desc)
}
// The game mechanics
var GameMechanics = [...]*MechanicInfo{
{"Avoiding Unkillable Objects",
"There are objects that the player cannot touch. These are different from normal enemies because they cannot be destroyed or moved."},
{"Race",
"The player must reach a place before the opponent does. Like \"Timed\" except the enemy as a \"timer\" can be slowed down by the player's actions, or there may be multiple enemies being raced against."},
}
// Get a random mechanic
func RandMechanic() *MechanicInfo {
i := rand.Intn(len(GameMechanics))
return GameMechanics[i]
}
var index = template.Must(template.ParseFiles("templates/index.html"))
func randMechHandler(w http.ResponseWriter, req *http.Request) {
mechanics := [3]*MechanicInfo{RandMechanic(), RandMechanic(), RandMechanic()}
if err := index.Execute(w, mechanics); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func main() {
http.HandleFunc("/", randMechHandler)
if err := http.ListenAndServe(":80", nil); err != nil {
panic(err)
}
}
这是访问冲突0xC0000005的windows代码。我在谷歌上搜索了一下,发现了一些关于杀毒软件和蠕虫的投诉。但是,您提到了32位机器和64位机器上的不同行为。您是否使用了为一种体系结构设计的二进制文件?@RayToal没有,我只为每种体系结构使用了在系统上编译的二进制文件。运行编译后的二进制文件时,我没有收到退出状态消息,尽管它以同样的方式失败;我想这是因为Windows的命令提示符没有显示退出状态。当我使用go run mechanics.go时,go工具显示退出状态。编辑:我也尝试过禁用我的防病毒软件,但我遇到了同样的问题。我喜欢你的初始化和生成方法!比我写的要干净得多。当我运行它时,仍然会收到相同的退出状态消息,但是
index.Execute
似乎可能就是源代码;这是一个比我以前更好的线索。