3
\$\begingroup\$

I am just learning how to use go and I'm a bit confused on how my code is supposed to be organised package wise.

Am I right to have the user package? I'm planning to redirect my /user/ routes to a handler in this package but I'm unsure if this is the go way to do things

My directory structure is

/go_rest_api
 /user
 -user.go
 -user_provider.go
 -app.go
 -main.go

main.go

package main
func main() {
 a := App{}
 a.Initialize()
 a.Run()
}

app.go

package main
import (
 "fmt"
 "log"
 "github.com/gorilla/mux"
 "gopkg.in/mgo.v2"
 "go_rest_api/user"
)
type App struct {
 Router *mux.Router
 Mongo *MongoConnection
}
type MongoConnection struct {
 Session *mgo.Session
}
func(a* App) GetMongoSession() *mgo.Session {
 return a.Mongo.Session.Copy()
}
func(a *App) Initialize() {
 session, err := mgo.Dial("127.0.0.1:27017")
 if err != nil {
 panic(err)
 }
 session.SetMode(mgo.Monotonic, true)
 a.Mongo = &MongoConnection{session}
 a.Router = mux.NewRouter()
}
func(a *App) Run() {
 defer a.Mongo.Session.Close()
 testMongo(a.GetMongoSession())
}
func testMongo(session *mgo.Session) {
 userProvider := user.Provider(session)
 err := userProvider.InsertUser(user.User{"test"})
 if err != nil {
 log.Fatal(err)
 }
 user := user.User{}
 err, user = userProvider.GetUser("test")
 if err != nil {
 log.Fatal(err)
 }
 fmt.Println("username:", user.Username)
}

user/user.go

package user
type User struct {
 Username string
}

user/user_provider.go

package user
import (
 "gopkg.in/mgo.v2/bson"
 "gopkg.in/mgo.v2"
)
type provider struct {
 Collection *mgo.Collection
}
func Provider(mongoSession *mgo.Session) provider {
 p := provider{}
 p.Collection = mongoSession.DB("test").C("user")
 return p
}
func(p *provider) InsertUser(user User) error {
 return p.Collection.Insert(&user)
}
func (p *provider) GetUser(username string) (error, User) {
 result := User{}
 err := p.Collection.Find(bson.M{"username": username}).One(&result)
 return err, result
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Feb 17, 2017 at 11:07
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Welcome to Code Review, your first post looks good, you should get some good answers! \$\endgroup\$ Commented Feb 17, 2017 at 11:35

1 Answer 1

2
\$\begingroup\$

You might wanted to start writing some comments on your code.

For example I've read all your code but there are some method that I don't understand like :

func(a* App) GetMongoSession() *mgo.Session {
 return a.Mongo.Session.Copy()
}

Okay we know It is getting the mongo session. but for what purpose?

// We get the mongo session for .... you can start explain why
func(a* App) GetMongoSession() *mgo.Session {
 return a.Mongo.Session.Copy()
}

I think it is a good think to keep comment the code, so others can understand it quickly. especially when someone who new to mongoDB. :D

answered Feb 17, 2017 at 15:27
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.