什么导致我的HTTP服务器失败,退出状态为“退出状态-1073741819”?

时间:2012-08-23 20:58:30

标签: http windows-7 go windows-7-x64

作为练习,我创建了一个小型HTTP服务器,可生成随机游戏机制,类似于this one。我是在Windows 7(32位)系统上编写的,它运行完美。但是,当我在家用机器Windows 7(64位)上运行它时,它始终会失败并显示相同的消息:exit status -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)
    }
}

此外,还有unabridged code_base.html templateindex.html template

可能导致此问题的原因是什么?是否有调试这样神秘退出状态的过程?

1 个答案:

答案 0 :(得分:1)

当我运行它时,我得到以下两个错误:

template: content:6: nil pointer evaluating *main.MechanicInfo.Name
http: multiple response.WriteHeader calls

前者在网络浏览器中,后者在我启动服务器的控制台窗口中。

nil指针问题是因为您的删节程序将GameMechanics [1:32]设置为nil。

第二个错误很有趣。你的http.ResponseWriter中的任何方法被调用的唯一地方是index.Execute,这不是你的代码 - 这意味着html / template中可能出现了错误。我正在使用Go 1.0.2进行测试。

我将_base.html放在index.html的顶部,然后将索引更改为:

var index = template.Must(template.ParseFiles("templates/index.html"))

并且http.WriteHeaders警告消失了。

不是一个真正的答案,而是一个你可以探索的方向。

作为奖励,这是编写程序的“走路”。请注意,我简化了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)
    }
}