Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I ran it, I got the following two errors:</p> <pre><code>template: content:6: nil pointer evaluating *main.MechanicInfo.Name http: multiple response.WriteHeader calls </code></pre> <p>The former was in the web browser, the latter in the console window where I launched your server.</p> <p>The nil pointer problem is because your abridged program leaves GameMechanics[1:32] set to nil.</p> <p>The second error is interesting. The only place in your program that any methods on your http.ResponseWriter get called is inside of index.Execute, which is not your code -- meaning maybe there is something wrong happening in html/template. I'm testing this with Go 1.0.2.</p> <p>I put _base.html at the top of index.html and then changed index to this:</p> <pre><code>var index = template.Must(template.ParseFiles("templates/index.html")) </code></pre> <p>and the http.WriteHeaders warning went away.</p> <p>Not really an answer, but a direction you could explore.</p> <p>As a bonus, here's the more "Go way" of writing your program. Note that I simplified the use of the PRNG (you don't need to instantiate unless you want several going in parallel) and simplified the structure initializer:</p> <pre><code>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) } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload