Never delete code. This is why you have git or svn, or whatever your tool of choice is. Never, ever delete code. You may think it's dumb, you may think it's crap, or useless or whatever, but in 2 years, you'll think. "Damn, I remember doing this already, don't I have some code in somewhere?" And you will. You may look at it and rewrite huge chunks because you're a far better programmer now, but trust me, re-writing c…
My poor use of golang's defer woke me up
11–20 of 25 posts
Re: My poor use of golang's defer woke me up
#12> [...] sudo less /var/log/upstart/app.log, 99999... oh, this log ACTUALLY has 99999 lines. Waiting, Waiting (note to self: google the command to jump to the end, there must be one). G (as in shift-g) jumps to the end of the file in less. Or use tail instead.
Also, less has build in help if you press 'h'.
Re: My poor use of golang's defer woke me up
#13Thanks for sharing.
Re: My poor use of golang's defer woke me up
#14Is there a club for people like us? I, too, do a bunch of work at two startups. I'm in the same exact situation, in fact. Thanks for sharing.
I think that's what HN kind of is...
Re: My poor use of golang's defer woke me up
#15Never delete code. This is why you have git or svn, or whatever your tool of choice is. Never, ever delete code. You may think it's dumb, you may think it's crap, or useless or whatever, but in 2 years, you'll think. "Damn, I remember doing this already, don't I have some code in somewhere?" And you will. You may look at it and rewrite huge chunks because you're a far better programmer now, but trust me, re-writing c…
grep -a 50 -b 50 string /dev/disk0 ftw
Re: My poor use of golang's defer woke me up
#16> [...] sudo less /var/log/upstart/app.log, 99999... oh, this log ACTUALLY has 99999 lines. Waiting, Waiting (note to self: google the command to jump to the end, there must be one). G (as in shift-g) jumps to the end of the file in less. Or use tail instead.
0%, 45%, etc work like you'd expect
Re: My poor use of golang's defer woke me up
#17Never delete code. This is why you have git or svn, or whatever your tool of choice is. Never, ever delete code. You may think it's dumb, you may think it's crap, or useless or whatever, but in 2 years, you'll think. "Damn, I remember doing this already, don't I have some code in somewhere?" And you will. You may look at it and rewrite huge chunks because you're a far better programmer now, but trust me, re-writing c…
Yep. I know. I even deleted the github repo. I'm seriously not sure what I was doing. Remove all traces of a bad idea?
Anyway, good blog post.
Re: My poor use of golang's defer woke me up
#18Here I thought this was going to be about defer and how it is error-prone compared to RAII and how it is a modern-day alloca with the same type of scope problems, like being unsafe to use in loops, and how it has a weird order of execution with arguments being evaluated immediately and statement evaluated later on.
Instead it's just about having poor project management. A missed opportunity I guess.
Re: My poor use of golang's defer woke me up
#19The second speaks of database connections:
http://jmoiron.net/blog/gos-database-sql/
The general approach:
1. Use a single DB connection, it will pool automatically
2. Use this pattern for all single row queries:
err = db.QueryRow(`...`, ...).Scan(&...)
if err == sql.ErrNoRows {
// Handle no rows
} else if err != nil {
// Handle actual error
}
// All fine
3. Use this pattern for all multi-row queries where you want to return a slice of structs containing the row values. Note that it is fine to call rows.Close() as soon as possible in addition to deferring it, defer takes care of handling it whenever something goes wrong and the explicit call returns the connection as soon as possible: rows, err := db.Query(`...`, ...)
if err != nil {
// Handle connection or statement error
}
defer rows.Close()
things := []rowStruct{}
for rows.Next() {
thing := rowStruct{}
err = rows.Scan(
&thing.id,
&thing.value,
)
if err != nil {
// Handle row parsing error
}
things = append(things, thing)
}
err = rows.Err()
if err != nil {
// Handle any errors within rows
}
rows.Close()
4. Use transactions as serial things, if you need to call another query whilst in a loop where you can't rows.Close(), then read the rows into a slice and range over the slice. You must never have two queries running in the same transaction... so code to do one thing before you do another, and be mindful of this if you are passing the transaction to other funcs.An extra bit of info:
5. defer doesn't just have to be used to call rows.Close(), if you want to know when things happen you can wrap the defer and log:
rows, err := db.Query(`...`,...)
if err != nil {
// Handle connection or statement error
}
defer func() {
log.Println(`Closing rows`)
rows.Close()
}()
On which point, beware there are some theoretically uncaught errors, for example tx.Rollback() can return an error http://golang.org/pkg/database/sql/#Tx.Rollback but if you have called it using defer tx.Rollback() after creating a transaction you'll never know. I hope that the only reason that might error is that something has already ended the transaction, but there is definitely scope for deferred finalisation within a func to cause errors that you might miss and it's worth considering the pattern above if you have any mysterious behaviour going on.Re: My poor use of golang's defer woke me up
#20> In my haste, I'd not noticed, I'm queueing up all of my rows close statements for the function end, which happens after the for loop, which opens way more than the allowed connection limit (about 100 in this case). Here I thought this was going to be about defer and how it is error-prone compared to RAII and how it is a modern-day alloca with the same type of scope problems, like being unsafe to use in loops, and h…
There is definitely something to be written on that, but - well I'm not the guy. Yet.