Create a ssl certificate (localhost.cert and localhost.key)You can ether generate a self signed cert: selfsignedcertificateor use mine: testHttp2ServerThis two files must be in the same directory as the server binary.

NOTE: http/2 in go ONLY WORKS WITH SSL/TLS! (currently)
I not sure why, but i did not got the server to support http/2 without TLS!
This could be a clientside or serverside issue - i dont know currently.

-- output should be something like this:"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0NDA4NjAzMzgsImlkIjoiYWRtaW4iLCJvcmlnX2lhdCI6MTQ0MDg1NjczOH0.cIIACBWOFCMZI4vBgSdQXkUsm8f7WpRoSoFtmoOtceQ"
--output

Thursday, June 18, 2015

How to use reflection to write into a slice of unknown items inside an unknown struct:

While working on Gorp with Indexes i had to solve the problem of how to write into a slice inside a struct passed as an interface{}. A new struct with the filled slice should be returned. Both the struct and the slice are not known at compile time, i only have the fieldname of the slice passed to me at runtime. After some serious headscratching and reading through the reflection code i luckily found a way to do it.

The basic method is:

Convert the incoming interface i to a reflect.Type t

Dereference until we have a t which is a Struct

Create a new reflect.Value v from the Type t

From v we now can ask for the FieldName of the slice, getting back another reflect.Value s

As we now have the slice s as a reflect.Value, we need to get the slice elements type

From the slice elements type we create a new instance of it (reflect.New)

Write to fields in this newItem using FieldByName (Hardcoded in the example)

Append the newItem to the slice (the set append was the hard part to find out for me)