I am building out a simple web server in Go. My goal is to expand on this framework in a different application I am building.

Here is my github repo https://github.com/jmacnc/gowebserver

Is what I have here using best practices?

I am specifically looking for feedback on the tests, and how I am connecting to different databases (testing and main application). As seen below...

//Handler for posting a new user
func (d *MongoDB) PostUser(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    //Retrieving request body
    var user User
    _ = json.NewDecoder(r.Body).Decode(&user)
    //Posting Company
    err := d.PostUserToDB(user)
    //Error Handling
    if err != nil {

//Populating a new user in the database
func (d *MongoDB) PostUserToDB(user User) error {
    _, err := d.db.Collection("users").InsertOne(context.Background(), user)
    if err != nil {
        return err
    return nil

func main() {
    //Connects to the database
    d , _ := ConnectToDB()
    //Creates mux router/multiplexer
    r := mux.NewRouter()
    //Handle Functions
    r.HandleFunc("/user", d.PostUser).Methods("POST")
    //Starts web server
    //Prints a string saying that the app is running...
    fmt.Println("Application Running...")
    log.Fatal(http.ListenAndServe(LOCALHOST, r))
  • \$\begingroup\$ Where are the tests and ConnectToDB logic tho? \$\endgroup\$ May 22, 2019 at 7:09
  • \$\begingroup\$ It the GitHub repo \$\endgroup\$
    – jmacnc
    May 22, 2019 at 11:33


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.