Go decode base64 image.. the easy way

I was banging my head against this for hours and then I found the solution.

While attempting to Decode a jpg image from base64 I kept getting a missing SOI marker error.

And then I found out that it’s way easier to just use image.Decode (the generic version from the “image” package directly) which will basically decode anything you throw at it 🙂

base64Data := b64.NewDecoder(b64.StdEncoding, strings.NewReader(userData))
img, str, err := image.Decode(base64Data)
if err != nil {
    log.Println("Error decoding image: " + err.Error() + " str: " + str)

Maybe this can save somebody a little time.

Connecting Go app to App Engine Cloud SQL (Postgres)

Just putting this here because I’ve been looking for hours without finding the right connection string. So basically if you’re using sqlx for example you’re going to need a connection string for your go app.

The easiest thing is to place this as an environment var in your app.yaml. In order to do this your app.yaml might look like this:

runtime: go111
  DATABASE_URL: "postgres://username:password@/databasename?host=/cloudsql/example:us-central1:example123"

After /cloudsql/ you’ll have your instancename (hint just run gcloud sql instances describe yourproject) from the command line and you’ll find a “connectionName” key, that’s the one

the trick was to 1) not use a host but /databasename directly and 2) using host= and not socket or unix_socket

N.B. This will work with a STANDARD GAE + Cloud Postgres

In the Go code it’s as simple as:

url := os.Getenv("DATABASE_URL")
DB, err = sqlx.Open("postgres", url)