Sitemap

Golang: recursive types

2 min readOct 26, 2026

--

Did you know that Go has recursive types?

Recursive types are also a fit for nested JSON, but the type needs a place for a leaf value. This works:

A recursive type is a type that refers to itself. That is useful for data structures:

type Node struct {
Next *Node
}

This shape is a list. A tree is the same idea with more than one link, for example Left and Right.

type Node struct {
Value any
Kids []Node
}

A map or a slice can be recursive too:

type M map[string]M
type S []S

Those compile, and every element is another map or slice.

There is no field for a string or a number, so they are not enough to parse real JSON on their own.

The same recursion is legal for a channel:

type Ch chan Ch

A value of type Ch carries only another Ch.

Get Mike’s stories in your inbox

Join Medium for free to get updates from this writer.

I can see how to use the map and slice forms. I still do not see a use for the channel.

The only ideas I can come up with are actors and games.

Here is my toy example:

package main

import "fmt"

type Step chan Step

const maxJumps = 3

func play(self Step, next Step, done Step) {
for range maxJumps {
self <- self
}

next <- self
done <- next
}

func main() {
step := make(Step)
next := make(Step)
done := make(Step)

named := map[Step]string{step: "step", next: "next", done: "done"}

go play(step, next, done)

for {
select {
case from := <-step:
fmt.Println(named[from], "->", "step")
case from := <-next:
fmt.Println(named[from], "->", "next")
case from := <-done:
fmt.Println(named[from], "->", "done")
return
}
}
}

Output:

step -> step
step -> step
step -> step
step -> next
next -> done

Each channel is a stone. Sending on a channel means landing on that stone, and the value sent is the stone you jumped from.

After a few jumps on the same stone (3 here), the player jumps to next and then to done.

I could use this in a PBEM game (have you ever played one?) or for a small FSM. One caveat: a PBEM turn carries the whole state of the world, and here there is no world at all, only the addresses of stones.

Still, I don’t think it’s for everyday programming: the same thing can be written in a more ordinary style.

Recursive maps, slices, and structs describe data. A recursive channel describes communication: what you pass is not data but an address to talk to next.

This is the core idea of the π-calculus, where processes send channel names over channels.

P.S. It’s also a great topic to bring up in interviews.

--

--

Mike
Mike

Written by Mike

I build distributed systems and real-time data pipelines. 10+ years in backend engineering. Focused on Kafka, microservices, and high-load systems. #golang