Golang: separate third party code from your own

I am using BorgBackup to backup my entire home, exluding some folders (cache, node_modules, etc).
I want to have a backup of all my code (even though I keep it on github and my private Gogs instance), so I didn’t want to exclude the entire GOPATH, but because of all the installed third-party dependencies, it grew pretty big in size.

Solution:

Multiple GOPATH entries.

You can use multiple directories as your GOPATH.

$ go env GOPATH
/home/me/.local/share/go:/home/me/code/go

The way this works, go get defaults to the first directory (so, ~/.local/share/go), so the third-party code lands there, and for my personal projects, I can manually create the directories in ~/code/go. Go im...


Golang: allow calling DELETE handlers via POST

I was designing an API recently and wanted to use all the fancy methods that HTTP provides.

One of those methods was DELETE.

The problem with all these methods is that I can’t send them from e.g. a form without javascript.

The solution I used was to add a fallback POST method handler.

To avoid code repetition, I created a little helper function which I wanted to share today:

func FauxDelete(f http.HandlerFunc) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		if r.Method == "POST" {
			r.Method = "DELETE"
		}

		f(w, r)
	}
}

This allows me to write a single handler for GET and DELETE (because most of work in both of those handler...

Git: include untracked and/or staged files in diffs

Include untracked files in a diff

git diff by default only takes into account the files that are already tracked. You may want to include untracked files to, e.g. when using git diff --stat to see how many lines are added and deleted.

To make git diff notice the untracked file you need to signal the

Intent to add

git add --intent-to-add [file]
git add -N [file]  # equivalend

What git add -N does is adds the [file] path to the index, without its contents (without staging it). Now, when git diff compares the file on disk to the index, all lines show up as newly added.

Include staged files in a diff

Another set of files that git diff ignores are the staged files. To change that, run ...

Golang: an int64 that serializes into JSON as a string
package utils

import (
    "encoding/json"
    "fmt"
    "strconv"
)

type ID int64

func (i ID) MarshalJSON() ([]byte, error) {
    return json.Marshal(fmt.Sprint(i))
}

func (i *ID) UnmarshalJSON(b []byte) error {
    var iStr string
    err := json.Unmarshal(b, &iStr)
    if err != nil {
        return err
    }

    val, err := strconv.ParseInt(iStr, 10, 64)
    if err != nil {
        return err
    }

    *i = ID(val)
    return nil
}

Because Javascript doesn’t support 64 bit integers (everything is stored as a float64), if you’re sending them as JSON in a JSON API response to the browser, you probably want to convert them to and from...

First update

First update

I ended my last post with

See you next Monday, then.

Of course, that was a lie, because I gave up before the week ended and didn’t have much to write about.

I did manage to do the required 3h of coding, and studied for about 2 hours, but that wasn’t even close to the goal.

Of course, that demotivated me even further so I just stopped caring again.

But I finally have an update for you!
I noticed that one of the biggest obstacles on my road to productivity is my smartphone.
I am addicted and can’t focus when I have Netflix and all of the social media within the reach of my hand.

To solve that, I asked my mother for help. We’ve agreed to designate...



N-th post

N-th post

This is not the first time I start a blog, so it’s not really a first post, but I guess I want to try again.

Every few months I realise I am wasting most of the time, and that time has come again. I’ve spent last week or so basically just watching Netflix all day, and finished the week by drinking till 4AM and then puking 4 kilograms out the next day. That probably means I should get my shit together, and I hope that documenting my journey here will help me do just that.

WELL THEN GET YOUR SHIT TOGETHER (gif)

By the way this blog is generated by a cool golang static site generator written by me, check it out.

There’s a few things I want to change.

Wha...