Update (19 November 2026): this is the full, updated version of my short May 2026 post "How Learning Go Taught Me to Learn Any Programming Language". That post stays up as it was; this one adds the real example.
The short version: I learned Go deeply, then had to write the same security-critical code again in Kotlin for an Android app. This post is the method I used to learn the second language in weeks instead of months, shown on real code: one 228-byte message format, written in both languages, side by side. The two implementations agree byte for byte because both are tested against one shared file of test vectors, and every test passes today in both.
Most of my Go learning was slow on purpose: memory, types, the runtime, concurrency, all from first principles. I wrote that up as ten strategies:
This post answers the question that list raises: does it actually transfer? In November I had to find out. Kengele, the open-source alarm for activists I build, has its core in Go: every check-in, panic and duress signal is a signed, encrypted, fixed-size 228-byte message. The activist's phone runs Android. So the same format had to exist in Kotlin, and it had to be byte-identical, because a Go guardian watcher has to open what a Kotlin phone sealed.
altradits / kengele
An alarm for when an activist goes missing. No servers, works through shutdowns. Swahili first.
Kengele
When an activist goes silent, the right people know within minutes, and nobody can switch the alarm off.
Ukinyamaza, kengele inalia. ("If you go quiet, the bell rings.")
Warning
Status: design stage. Not for real use. There is no working app yet. Do not rely on Kengele to protect anyone until an independent security review is complete and published here. People's lives may depend on this tool, so we will not cut corners to ship it sooner.
Why
When an activist disappears, how fast the public finds out often decides how quickly they are freed.
- In Kenya, more than 80 people were abducted or forcibly disappeared after the 2024 protests. In January 2025, abducted young men were released within hours of public outcry.
- Boniface Mwangi and Agather Atuhaire, detained in Tanzania in May 2025, were released after their disappearance became public.
- Governments switch networks off when it…
Contents
- The method in five steps
- Step 1: write down what must not change
- Step 2: map concepts, not syntax
- Side by side: the signed plaintext
- Trap 1: Kotlin bytes are signed
- Trap 2: there is no uint32
- Trap 3: errors become exceptions
- Trap 4: enums and constants
- Step 3: let one file judge both
- Step 4 and 5: tests first, then idioms
- What transferred, and what didn't
The method in five steps
- Write down what must not change, independent of any language: the spec.
- Map concepts, not syntax. For each concept the code relies on (fixed-width integers, byte arrays, errors, enums), find the new language's equivalent and, more importantly, where it differs.
- Let one neutral file judge both implementations. Test vectors generated once, checked by both.
- Port the tests before the code, then make them pass.
- Only then make it idiomatic. First correct, then natural-looking.
Step 1: write down what must not change
The format lives in a document (docs/message-format.md), not in either language. Its core is one table:
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | Kind |
| 1 | 4 | Created at: Unix seconds, big-endian |
| 5 | 4 | Sequence number, big-endian |
| 9 | 1 | Body length, 0 to 86 |
| 10 | 86 | Body, then zero padding |
| 96 | 64 | BIP-340 signature over bytes 0 to 95 |
That 160-byte plaintext is then NIP-44 encrypted to 227 bytes, plus a version byte: 228. When the spec is written down like this, "porting" stops meaning "translating Go" and starts meaning "implementing a table". That's a much easier problem, and it makes the Go code a reference rather than a source to transliterate.
Step 2: map concepts, not syntax
Before writing any Kotlin, list every concept the Go code depended on and find its Kotlin counterpart, noting where the two differ. For this port:
| Concept | Go | Kotlin (JVM) | Difference that matters |
|---|---|---|---|
| A byte | byte = uint8, 0 to 255 | Byte, −128 to 127 | Signed. Every read needs masking |
| 32-bit unsigned | uint32 | none in the signatures used; use Long | Range must be checked by hand |
| Byte array | []byte (slice: shares memory) | ByteArray (a reference; copyOfRange copies) | Different copy semantics |
| Fixed-size array | [32]byte (a value) | ByteArray of size 32 (a reference) | No compiler-checked sizes |
| Errors | returned error values | thrown exceptions | Control flow changes shape |
| Enum | const + iota on a named type | enum class with a value | Lookup from a byte needs code |
| Struct | struct | data class | equals on ByteArray fields is by identity |
| Big integers | btcec library | java.math.BigInteger | The port does secp256k1 itself |
The right-hand column is the whole lesson. Syntax maps in an afternoon. Differences in semantics are where ports go wrong, and each row above became a test.
Side by side: the signed plaintext
Here is the same function in both languages. Go, from internal/message/message.go:
func sign(e Event, sender *btcec.PrivateKey) ([]byte, error) { if sender == nil { return nil, errors.New("message: nil sender key") } if !e.Kind.Valid() { return nil, ErrKind } if len(e.Body) > MaxBodySize { return nil, ErrBody } ts := e.CreatedAt.Unix() if ts <= 0 || ts > int64(^uint32(0)) { return nil, ErrTime } inner := make([]byte, innerSize) inner[0] = byte(e.Kind) binary.BigEndian.PutUint32(inner[1:5], uint32(ts)) binary.BigEndian.PutUint32(inner[5:9], e.Seq) inner[9] = byte(len(e.Body)) copy(inner[10:10+MaxBodySize], e.Body) h := signedHash(inner[:signedSize]) sig, err := schnorr.Sign(sender, h[:]) if err != nil { return nil, fmt.Errorf("message: sign: %w", err) } copy(inner[signedSize:], sig.Serialize()) return inner, nil } Kotlin, from KengeleMessage.kt:
fun sign(event: Event, senderPriv: ByteArray, aux: ByteArray? = null): ByteArray { if (event.body.size > MAX_BODY_SIZE) throw MessageException("body too large") if (event.createdAtSecs <= 0 || event.createdAtSecs > 0xFFFFFFFFL) { throw MessageException("invalid timestamp") } if (event.seq < 0 || event.seq > 0xFFFFFFFFL) throw MessageException("invalid seq") val inner = ByteArray(INNER_SIZE) inner[0] = event.kind.value.toByte() putU32(inner, 1, event.createdAtSecs) putU32(inner, 5, event.seq) inner[9] = event.body.size.toByte() event.body.copyInto(inner, 10) val sig = Secp256k1.schnorrSign( senderPriv, signedHash(inner.copyOfRange(0, SIGNED_SIZE)), aux, ) sig.copyInto(inner, SIGNED_SIZE) return inner } Same shape, same offsets, same checks in the same order. Notice the one check Kotlin has that Go doesn't: event.seq must be between 0 and 0xFFFFFFFF. In Go, Seq is a uint32, so the type system enforces that range. In Kotlin it's a Long, so the code has to. That's row 2 of the table, showing up exactly where predicted.
Trap 1: Kotlin bytes are signed
In Go, a byte holding 0xC8 is 200. On the JVM, the same bits in a Byte are −56. Convert it to a Long naively and the sign bit spreads into every upper bit:
val b: Byte = 0xC8.toByte() b.toLong() // -56 = 0xFFFFFFFFFFFFFFC8 b.toLong() and 0xFF // 200 = 0xC8 So reading a big-endian uint32 masks every byte before shifting:
private fun getU32(b: ByteArray, o: Int): Long = ((b[o].toLong() and 0xFF) shl 24) or ((b[o + 1].toLong() and 0xFF) shl 16) or ((b[o + 2].toLong() and 0xFF) shl 8) or (b[o + 3].toLong() and 0xFF) Drop one and 0xFF and every timestamp with a high byte at or above 0x80 decodes wrong. Today's Unix times start with 0x69 (1768486020 is 69 68 f4 84), so a bug in the top byte wouldn't show up until 2038, while a bug in a lower byte shows up for roughly half of all timestamps. That's exactly the kind of bug that passes a quick test and fails in production, and exactly why the shared vectors (below) include full 32-bit values.
The same mask appears when reading the kind and body length: inner[0].toInt() and 0xFF. Go needs none of it.
Trap 2: there is no uint32
Go writes four bytes with one call: binary.BigEndian.PutUint32(b, v), and v can't be negative or larger than 2³² − 1 because of its type. Kotlin writes them by hand with unsigned shifts:
private fun putU32(b: ByteArray, o: Int, v: Long) { b[o] = ((v ushr 24) and 0xFF).toByte() b[o + 1] = ((v ushr 16) and 0xFF).toByte() b[o + 2] = ((v ushr 8) and 0xFF).toByte() b[o + 3] = (v and 0xFF).toByte() } ushr (unsigned shift right) fills with zeros; shr would fill with the sign bit. For values already checked to be in range it makes no difference, but ushr is the habit to keep, because the range check and the shift are in different functions and can drift apart. (Kotlin does have UInt, but the JVM crypto APIs work in signed types, so a range-checked Long keeps conversions in one place.)
Trap 3: errors become exceptions
Go's Open returns (*Event, error), and the caller decides. Trying each possible sender's key is a loop that continues on error:
for _, s := range senders { ck, err := conversationKey(recipient, s) if err != nil { continue } plain, err := nip44.Decrypt(b64, ck) if err != nil { continue // wrong sender or altered message: try the next key } return parse([]byte(plain), s, now) } return nil, ErrNoSender Kotlin throws, so the same loop needs try/catch around exactly the calls that may fail with "wrong key", and nothing else:
for (sender in senders) { val inner: ByteArray try { val ck = Nip44.conversationKey(recipientPriv, sender) inner = Nip44.decrypt(payload, ck) } catch (e: MessageException) { continue // Wrong sender or altered message: try the next key. } return parse(inner, sender, nowSecs) } throw MessageException("not addressed to this key by any known sender") The detail to get right: parse is outside the try. If decryption succeeds but the signature is bad, that's not "wrong sender, try the next one", it's a forged or corrupted message, and it must fail. Wrapping too much in the try would silently skip a forgery and report "unknown sender" instead. Go's explicit error handling made that distinction obvious; in Kotlin I had to preserve it deliberately. Catching only MessageException (not Exception) is the same idea: a bug like an index error must not be mistaken for "wrong key".
Trap 4: enums and constants
Go derives sizes from each other, so a change to one breaks the build of the others:
innerSize = 1 + 4 + 4 + 1 + MaxBodySize + schnorr.SignatureSize // 160 nip44Size = 1 + 32 + 2 + innerSize + 32 // 227 WireSize = 1 + nip44Size // 228 The Kotlin port writes them as literals (INNER_SIZE = 160, WIRE_SIZE = 228) and protects them differently: a test named constantsMatchVectors checks every constant against the shared vector file, which Go generated from its derived values. Two different mechanisms, one guarantee.
Enums: Go's kinds are typed constants, and validity is a map lookup. Kotlin's enum class Kind(val value: Int) reads more naturally, but turning a byte from the wire back into a Kind needs a lookup that throws on unknown values, because a silently defaulted kind could turn a panic into a check-in:
companion object { fun fromValue(v: Int): Kind = values().firstOrNull { it.value == v } ?: throw MessageException("unknown kind $v") } Step 3: let one file judge both
The single most useful decision: neither implementation is the reference. A file is. Go generates internal/message/testdata/vectors.json with made-up keys, and both sides test against it:
// Shared test vectors for the Kotlin port (android/, issue #15). // // testdata/vectors.json is the single source both sides test against: this // file checks every value against the live Go implementation, and the // Android unit tests read the same file. The vectors pin every layer separately: the constants, the x-only public keys, the ECDH conversation key, the ChaCha20 keystream, the tagged hash, the signature, and the final 304-character SMS text. When something disagrees, the failing layer tells you where. That mattered, because the Kotlin port implements secp256k1 and BIP-340 itself in plain JDK code (BigInteger), so the app has no native crypto dependency. Without layer-by-layer vectors, a mismatch in the final 304 characters would be nearly impossible to trace.
Both sides, run today:
go test -count=1 -v -run 'SharedVectors|GoldenVector' ./internal/message --- PASS: TestGoldenVector (0.01s) --- PASS: TestSharedVectorsFormat (0.00s) --- PASS: TestSharedVectorsGolden (0.00s) --- PASS: TestSharedVectorsNIP44 (0.00s) --- PASS: TestSharedVectorsChaCha20 (0.00s) ok github.com/altradits/kengele/internal/message 0.561s cd android && ./gradlew :message:test # needs ANDROID_HOME set KengeleMessageTest tests=17 failures=0 errors=0 Nip44Test tests=5 failures=0 errors=0 Secp256k1Test tests=6 failures=0 errors=0 ChaCha20Test tests=2 failures=0 errors=0 Step 4 and 5: tests first, then idioms
The Kotlin tests mirror the Go tests' names and intent: goldenVectorSealsToPinnedText, everyKindHasTheSameLength, rejectsNonZeroPadding, rejectsGuardianForgingActivist, rejectsFutureTimestamp. Each one is a rule from the spec, so a red test meant "this rule isn't implemented yet", which is a much better to-do list than "translate the next function".
The idiomatic parts sit on top of that: a data class Event with default arguments, an object for the singleton, copyInto instead of index loops, require for argument checks. Idioms are the last step because they're the easiest to change safely once tests exist, and the most dangerous to start with: "idiomatic" first drafts tend to hide exactly the semantic differences in the table above.
What transferred, and what didn't
Transferred completely: memory thinking (what's a value, what's a reference, what gets copied), fixed-width integer thinking, the habit of strict parsing, and treating tests as the specification. Every one of those came straight from learning Go from first principles, and none of it is Go-specific.
Didn't transfer, had to be learned: signed bytes, the absence of compiler-checked array sizes, exceptions as control flow, and how JVM crypto APIs want their inputs. That list stays short when you write step 2's table before writing code.
The lesson I'd give anyone learning a second language: don't learn the new syntax; learn the new differences. Make a table like the one in step 2 on day one, and turn every row into a test.
The ten strategies are the theory; this was the exam. A hands-on companion with ten predict-the-output exercises:
If you're interested in systems engineering, Go internals and first-principles learning, follow along. I'm building Altradits, an open-source and Bitcoin-learning home, in public.

![Physical AI Foundations and Definitions — Ch.08: Physical AI Product Strategy [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F4gyormoamcida6t18mdw.png)
![Physical AI Foundations and Definitions — Ch.07: Relation to AMR Manipulator Humanoid Quadruped UAV [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fte7aqbjpqzqiay6cht0x.png)
![Physical AI Foundations and Definitions — Ch.05: Physical AI Taxonomy [EN]](https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fxeb66tbhskewzsz49i7p.png)