Looking for CRUD implementation example.

440 views
Skip to first unread message

mm

unread,
Apr 20, 2015, 12:22:29 PM4/20/15
to golan...@googlegroups.com
Could someone point me to a nice implementation of how CRUD is used in GO? So far I have found this -
but it is incomplete. I come from Python, so I really need an example of how CRUD is used in GO.

thanks

DV

unread,
Apr 20, 2015, 1:26:52 PM4/20/15
to golan...@googlegroups.com
This is a strange question. 

You're looking for an example that demonstrates how to do Create, Update, Delete with a relational database in Go? You can start here: http://golang.org/pkg/database/sql/
Or are you looking for an ORM? 

mm

unread,
Apr 20, 2015, 1:41:57 PM4/20/15
to golan...@googlegroups.com

Sorry for being so oblique. Im using mgo/mongodb
I am trying to understand, specifically how to do GET and POST... usually in python I have something like this:
class Edit(BaseHandler, Jinja2Rendering):

    @auth_admin
    def get(self, user_id):
        """Display a user profile
        """
Not sure how to do that in my func(c *context) UserEdit(rw web.ResponseWriter, req *web.Request) {}

On my list page I have the list of users and when you click on one you get to the user edit page.
I get from my list page to my edit page like so

rootRouter.Get("/user/edit/:user_id", (*Context).UserEdit)
In python I get the user_id from params. How does that work in go?

Egon

unread,
Apr 20, 2015, 2:46:36 PM4/20/15
to golan...@googlegroups.com
Read http://golang.org/doc/articles/wiki/ it has an example how to use FormValue.
Then http://astaxie.gitbooks.io/build-web-application-with-golang/content/ for a thorough explanation how to build web apps with Go.

+ Egon

mm

unread,
Apr 20, 2015, 3:38:22 PM4/20/15
to golan...@googlegroups.com
Ok found what I am looking for...Using Gocraft:

You can capture path variables like this:

router.Get("/suggestions/:suggestion_id/comments/:comment_id")

In your handler, you can access them like this:

func (c *YourContext) Root(rw web.ResponseWriter, req *web.Request) {
    fmt.Fprint(rw, "Suggestion ID:", req.PathParams["suggestion_id"])
    fmt.Fprint(rw, "Comment ID:", req.PathParams["comment_id"])
}

You can also validate the format of your path params with a regexp. For instance, to ensure the 'ids' start with a digit:

router.Get("/suggestions/:suggestion_id:\\d.*/comments/:comment_id:\\d.*")

mm

unread,
Apr 20, 2015, 3:39:07 PM4/20/15
to golan...@googlegroups.com
Thanks so much Egon, those are great links for me!!

Guillermo Estrada

unread,
Apr 20, 2015, 4:51:12 PM4/20/15
to golan...@googlegroups.com
You're probably looking how to do an app using a framework (router at least) and a database backend. I read you use MongoDB in Python, so for that you should use the excellent Mongo library by Gustavo Niemeyer:


As for frameworks, depending on the level of the app you want to code there are a LOT, if you are only looking for a router and stuff to use with Go's standard lib... I can definitely recommend Gorilla Toolkit (http://www.gorillatoolkit.org/) you want to be using the MUX package at least, it supports everything from subdomains to regular expression routing.

As for frameworks, Martini, Gin, and Goji are the simplest to get into IMHO.


Most of them have great documentation to get you started, as for tutorialesque writing in this matter a simple Google search yields some of them (once you know what to look for).



mm

unread,
Apr 20, 2015, 8:07:41 PM4/20/15
to golan...@googlegroups.com
Thank you so much Guillermo, yes I need to keep it light so am just  using mgo and gocraft. I am really liking Go so far. Its a little complex for me but once it compiles I know I have something sure and lightning fast. 

Egon

unread,
Apr 21, 2015, 4:42:06 AM4/21/15
to golan...@googlegroups.com
Also forgot... two must videos to watch:

mm

unread,
Apr 22, 2015, 4:35:54 PM4/22/15
to golan...@googlegroups.com
Thank you Egon! Super helpful links !
Reply all
Reply to author
Forward
0 new messages