re: How I Built an API with Mux, Go, PostgreSQL, and GORM VIEW POST

VIEW FULL DISCUSSION
 

Nicely done. Simple and clean.

But I've some tips for you:

  • The GOPATH and GOROOT setup isn't necessary. GOPATH default is $HOME/go since Go 1.8. You can check where your GOPATH and GOROOT points to with "go env". Only need to set the PATH:


export PATH=$HOME/go/bin:/usr/local/bin:$PATH

  • You must validate the return of json.NewEncoder(w).Encode(&resource). Always check your return errors.

  • db.Create, db.First, db.Find, etc can fail, so db.Error must be checked.

if err := db.Create(&resource).Error; err != nil {
    // error handling...
}

Keep learning and sharing! :)

 

Interesting -- go install and godep didn't work before I set the GOPATH and GOROOT -- I would guess it is because of brew install instead of following the website directions.

I would check the errors on a bigger app -- the chances of this one being used are next to zero! More a learning experience than anything.

Thanks for the tips!

code of conduct - report abuse