Application crashes if not run in "source" directory #2

Open
opened 2018-02-01 13:37:47 -05:00 by raff · 0 comments
raff commented 2018-02-01 13:37:47 -05:00 (Migrated from github.com)

The application tries to load the images from the current directory, and if they are not there it crashes.

Best thing would be to embed the images in the binary (there are a few packages to do that, like https://github.com/jteeuwen/go-bindata) but at least checking for the error would be good.

Also, when the crash happen, the terminal is left in a "bad state". I fixed by moving the termbox.Close() in a defer statement:

`diff --git a/main.go b/main.go
index f3aee8d..89bc0ef 100644
--- a/main.go
+++ b/main.go
@@ -96,6 +96,11 @@ func main() {
alienDirection := 1 // direction where alien is heading
score := 0 // number of points scored in the game so far

  •    defer func() {
    
  •       termbox.Close()
    
  •       fmt.Println("\nGAME OVER!\nFinal score:", score)
    
  •    }()
    
  •   // poll for keyboard events in another goroutine
      events := make(chan termbox.Event, 1000)
      go func() {
    

@@ -243,8 +248,6 @@ start:
fmt.Println("\n\nSCORE:", score)
loop++
}

  •   termbox.Close()
    
  •   fmt.Println("\nGAME OVER!\nFinal score:", score)
    

}
`

The application tries to load the images from the current directory, and if they are not there it crashes. Best thing would be to embed the images in the binary (there are a few packages to do that, like https://github.com/jteeuwen/go-bindata) but at least checking for the error would be good. Also, when the crash happen, the terminal is left in a "bad state". I fixed by moving the termbox.Close() in a defer statement: `diff --git a/main.go b/main.go index f3aee8d..89bc0ef 100644 --- a/main.go +++ b/main.go @@ -96,6 +96,11 @@ func main() { alienDirection := 1 // direction where alien is heading score := 0 // number of points scored in the game so far + defer func() { + termbox.Close() + fmt.Println("\nGAME OVER!\nFinal score:", score) + }() + // poll for keyboard events in another goroutine events := make(chan termbox.Event, 1000) go func() { @@ -243,8 +248,6 @@ start: fmt.Println("\n\nSCORE:", score) loop++ } - termbox.Close() - fmt.Println("\nGAME OVER!\nFinal score:", score) } `
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
sausheong/invaders#2
No description provided.