George Opritescu

Developer from somewhere

adding Println statements in golang stdlib

  • install go bootstrap: https://storage.googleapis.com/golang/go1.4-bootstrap-20161024.tar.gz
  • build it ( note the directory it says at the end of installation), in my case:
Installed Go for darwin/amd64 in /Users/geo/bootstrapped-go
  • clone go repo: https://go.googlesource.com/go
  • check out the version you’re interested in, in my case go1.7.4
  • go to src folder
  • GOROOT_BOOTSTRAP=/Users/boostrapped-go ./make.bash
  • add your Println statements where needed
  • any time you modify something in stdlib, you need to recompile
  • repeat until problem found
Read More

chrome and chunked responses

Had an issue when trying to react to a chunked response from a server. The onreadystatechange function was being called only at the end, and not for intermediate values. Found the answer here:

  • turns out, you need to set the header X-Content-Type-Options to nosniff
Read More

postgresql timeout

Found a really interesting bit of info in this heroku article:

SET statement_timeout TO '30s';

Which would ensure your queries will be timed out after 30 seconds.

Read More

golang unix timestamp

Found answer here

int32(time.Now().Unix())
Read More

golang goqu ignore struct field

Problem: unexported struct field was used in a goqu query. The field did not have an equivalent db column.

Solution: mark it like in JSON:

type someName struct {
  myVirtualField int `db:"-"`
}
Read More

golang switch case fallthrough

TIL: in go, a switch cases don’t automatically fallthrough. So, this:

switch something {
  case 0:
  case 1:
    fmt.Println("handling both")
}

won’t work as intended. Instead it should be:

switch something {
  case 0:
    fallthrough
  case 1:
    fmt.Println("handling both")
}
Read More

golang debug specific test from compiled binary

Problem: want to debug a failing test, from a compiled binary containing a lot of other tests.

Solution:

  • compile tests with go test -c
  • run dlv: dlv exec ./my.test – -test.run SomePattern
  • debug!
Read More

golang blank struct when unmarshalling json

TIL: if you have a struct with unexported fields ( aka lowercase field names ), and you try to unmarshal it from another package, the result will be a struct with blank field names.

Read More

golang database column tags

Comes in handy when you want a struct field to map to a db column, which may have a different name. For example, I have CreatedAt field, but in the db, it’s created_at:

type Agent struct {
	Name      string
	Config    string
	CreatedAt time.Time `db:"created_at"`
}
Read More