Notes on codes, projects and everything

Over-doing a task: reading CSV file in #golang

I was invited to try Go (the programming language, not that board game) a few months ago, however I didn’t complete back then. The main reason was because it felt raw, compared to other languages that I know a fair bit better (for example Ruby). There was no much syntatic sugar around, and getting some work done with it feels “dirty”.

Few months later I got myself in a new environment, and was asked to pick up golang (will just refer Go as golang for searchability) for the efficiency. Therefore, this post is very much about my first impression towards the language, and development environment. There might be some mis-conception or mis-understanding scattered around, and hopefully in time I get to have a more proper impression with it.

Setting it up on a OS X environment is mostly painless with homebrew, as it is mostly just a brew command away and getting the environment variable exported properly. Apparently I should not scatter my source code everywhere I like and should just put them in one place. Considering the default set up requires code to be compiled before use, and the set up actually compiles all the binaries to the same place. This takes some time to get used to, as most of the languages I used in the past don’t generally work this way. I meant most of them has an official implementation in the form of interpreter (or on the fly compilation? or even some weird combination of both), so I could just run the source anywhere I like.

Anyway, in short at this state I still don’t find golang pretty. However, that’s also because I still don’t see myself being productive with it. So in order to get used to the syntax, is to write something that works. I was thinking to port the Dragon Curve scripts, but something got in the way when I was trying to do it yesterday. So instead of a fun little project, I wrote something that is more related to my first task in this new environment instead: reading a CSV file.

So the current state of the program (so tempted to call it a script, but it is obviously not) reads a csv file, and organized into a slice of hash map. The end goal is to make them into JSON, but I still can’t wire my head properly, so that would be part of the next phase. Also the code here is not concurrency ready (speaking of this, I am pretty sad seeing map/reduce/filter is never ever gonna implemented here, due to generics, and I only first heard of generics yesterday).

package main
import (
type record_builder func([]string) map[string]string
func csv_get_reader(path string) func() ([]string, error) {
file, file_error := os.Open(os.Args[1])
if file_error != nil {
file_reader := csv.NewReader(file)
return func() ([]string, error) {
return file_reader.Read()
func record_get_builder(field_names []string) record_builder {
return func(record_raw []string) map[string]string {
record := map[string]string{}
for index, field := range record_raw {
record[field_names[index]] = field
return record
func main() {
var builder record_builder
reader := csv_get_reader(os.Args[1])
records := []map[string]string{}
for i := 0; ; i++ {
record_raw, record_error := reader()

if record_error == io.EOF {
} else if record_error != nil {
if i == 0 {
builder = record_get_builder(record_raw)
} else {
records = append(records, builder(record_raw))

Putting those meta stuff aside, the experience of writing a golang program is actually quite a rewarding one. I am personally a vim user, so basically I installed vim-go with my own vim-manager script. Practically that’s pretty much all the things one needs to do to get the editor ready for golang development. I also installed goimports to assist auto-formatting, and the tool is awesome, and feels evil at times. It is awesome because it gets everything formatted properly, and even fill in the import statement properly. It is evil also due to the same reason, but let’s forget that as it made my life easier. With everyone uses that (which is part of the best-practice iirc), it probably doesn’t matter if it uses tab instead of spaces.

Whether it is good at concurrency, that still has to wait till I am able to produce the next version of this script. Hopefully this doesn’t take too long considering I spent quite some time reading through the doc on Python’s co-routines early this year (it is too complicated for me to summarize it and jot down here though, I still finding problems in explaining it clearly, or should I scan and post my handwritten notes?).

leave your comment

name is required

email is required

have a blog?

This blog uses scripts to assist and automate comment moderation, and the author of this blog post does not hold responsibility in the content of posted comments. Please note that activities such as flaming, ungrounded accusations as well as spamming will not be entertained.


Click to change color scheme