Monday, September 19, 2016

How to parse JSON w/ a field which can be string or number in Go

In this post, I'm going to explain how to parse JSON data with a field which can be either a string or number in Go.

Go has a great support for JSON. In many cases what you have to do is to define corresponding `struct`s to parse JSON.

Let's say we're dealing with a Web API provided by some other company. It gives us a series of name-value pairs in this format:

We can assume `name` is a string and `value` is a number. Thus, the corresponding `struct` will be like this:

Now we can parse the JSON like this:

Cool. Then suddenly you realize that `value` isn't always a number- it can be a string:

Now it's f'ed up. But no worries. We can handle this situation by making `Value` either string or number instead of just string:

Now we can handle a string-or-number JSON field.

This is based on my real life experience and now I'm seeing a case where `Value` can also be an object, which I don't know how to handle gracefully. Life is tough.

Friday, March 1, 2013



思えば4年前無職で引きこもってたときに、 無駄に元気なtks氏らに出くわしたのが入社のきっかけでした。



入社早々、僕が少々Lispを書くと聞きつけたtks氏が、todesking氏と引き合わせてくれ、 結果として現在まで続く「todesking枠」として社内各位に白い目で見られるきっかけになったりしました。


  • 研究開発プロジェクトのドキュメントの翻訳マネジメント
  • 世界的にヤバい日本企業のサイトの検索の機能改善
  • 某メディア向けTwitterクローンの機能改善
  • 同上のOAuth認証機能付きRESTful APIの開発
  • 自然言語処理を応用した投資家向けニュース解析の機能改善


前職と、そしてラボでの4年間を通して、 受託開発という形でプログラミングを仕事としてきましたが、 次第に「プログラミングを仕事にする」ということに対して、 もう少し違ったアプローチをしてみたいと思うようになりました。




Sunday, January 13, 2013



ocamlscriptで実行させる設計にしたため、ヘッダーが付いてるのと、 現行のBatteriesではBatStrモジュールが削除されていて、正規表現が扱い辛いのでこの長さ。


Friday, January 6, 2012

XOR texture in Common Lisp

Use the Right Tools for the Right Problems. (I mean C)

Tuesday, January 3, 2012

IBNIZ -- a VM for generative art

This demo IBNIZ - a hardcore audiovisual virtual machine and an esoteric programming language blowed my mind. It's a generative audio/visual environment and very awesome. I want one like this dedicated to me.. this year.

Sunday, November 13, 2011

How to know if CEOs are really dead

Say you read about a CEO's death on Twitter and you want to know if it's real or false rumor. It's a bad choice to go check Wikipedia since it can be edited by random people.

Here's a simple BASH one-liner you can use in this situation:

[ $(curl -s --head http://${host}/${first_name}${last_name}/ | grep HTTP | cut -d' ' -f2) -eq 200 ] && echo 'RIP' || echo 'still alive'

Try these:

[ $(curl -s --head | grep HTTP | cut -d' ' -f2) -eq 200 ] && echo 'RIP' || echo 'still alive'
[ $(curl -s --head | grep HTTP | cut -d' ' -f2) -eq 200 ] && echo 'RIP' || echo 'still alive'

Friday, November 11, 2011

How I listen to Internet

These days, I'm listening to Internet while at work.

At the beginning, it was just like tcpdump | sox -traw -r64k -c1 -eunsigned -b8 - -d.

# For those who doesn't know sox, it's a command that you can install from Homebrew.
# It's same as > /dev/audio in Linux.

Then I found that it could be mixed with Experimental music from very short C prog.

Now I'm listening to this:

EDIT: And this is how it sounds while i'm watching

Internet by Yutaka Ichibangase