Categories
Uncategorized

Managing Golang Workspace: My ZSH Configuration

One of the first thing beginners wonder in the Go userland is how to organize a project. Go expects to have a global workspace. A go workspace contains 3 directories: “src”, “pkg” and “bin”. The src directory contains the source codes for all your projects and their dependencies. When you try to import a package, go compiler will try to locate it under this “src” directory. The “pkg” directory is used to build cross OS packages out of your source code. Yes, you can build single compiled binaries for other platforms directly from your current OS. That is one of the awesome features of Golang. Finally, the “bin” directory holds all the executable binaries. When you’re building a command line app or trying to install one from 3rd parties using the “go install” command, the binary is placed in here. I have the central Go Workspace in ~/Projects/golang.

Go expects the path of the go workspace as the $GOPATH environment variable. So I added this to my ZSH profile:

My $GOPATH is set to ~/Projects/golang:

Now that we have a workspace, how do we lay out individual projects? We can choose any structure we like. As long as Go can find required packages in the workspace’s src directory tree, it will be okay. However, since we all love to push our codes to some code hosting service, we will try to match our project structure with our VCS url structure. How? Well, first, we know that Go has a fantastic tool – “go get” – it can download source codes from an array of VCS and install them for us. How does it work? When I type: “go get github.com/masnun/go-project”, it understands that it’s a git project. It would create the reflecting directory structure inside the “src” directory of the workspace so that we can import the project as “import github.com/masnun/go-project” in our local projects. So it’s better if we keep our own projects in a similar pattern whether we publish them or not in the future.

I created a directory named “github.com” under “src” and then created “masnun” under that. Now my projects would reside in: ~/Projects/golang/src/github.com/masnun directory. My newly created project path would be something like: ~/Projects/golang/src/github.com/masnun/go-project-name. I can now treat ~/Projects/golang/src/github.com/masnun directory as my personal workspace. So, I added these to my .zshrc for convenience:

After I have this mirrored up, I can actually do:

Then I added a handy alias to emulate Python’s virtualenvwrapper’s “workon” command:

So I can now just do:

I also added the ~/Projects/golang/bin (aka $GOPATH/bin) to my system path so that I can use the commands installed from “go install” or “go get”.

How do you organize your Go projects? What tools/techniques do you use in day to day Golang hacking?

Categories
Uncategorized

Copying (duplicating) MongoDB Documents (Rows)

The use case is simple – I have one row (I mean document). I need to make multiple copies of it. Say, I am building a blog app and I need to quickly generate a few posts on the database. Scripting is one way to do it, but if I need to do this only once, automation is a waste of time.

Thanks to mongodb’s shell and JS-like APIs. This is actually simple.

Connect to mongodb:

Select the database:

Find an entry on the “posts” collection:

Now let’s change the “_id”:

X is now a whole new object/document (since we changed the “_id”). We can alter other fields as well. Let’s store it:

You just copied one document into a new document. Cool?

The command line mongo client has readline and history support. So you can actually use the up arrow to repeat the commands quite easily.

Categories
Uncategorized

Vim Madness

I use the top notch IDEs for development but that is not geeky enough. So, ignoring my previous failed attempts, I am going to give the text editor one more try. Apparently, this blog post would be more like self documentation, mostly notes and hints to self. If anyone else also gets benefited, that’d be an added bonus.

Quick Shortcuts

  • Vundle Bundle Installation: “:BundleInstall”
  • New Tab: “:tabnew
  • Navigate Tabs: “:tabn” for next, “:tabp” for prev. “gt” in normal mode
  • Switch Windows: “Ctrl + w, Ctrl+w”
  • Splitting Windows: “:split “, “vsplit” for vertical splits
  • NerdTree – Open in new tab: “t”

Let there be ~/.vimrc

OK, here’s my ~/.vimrc, the initial version is messy, not organized at all, everything I came across, I stuffed in here. I shall update and clean the vimrc over time.