Backend Development 9 min read

Recreating the Spring Festival Gala Magic Trick Using Go

This article walks through a step‑by‑step Go implementation of the popular Spring Festival Gala magic trick, explaining the underlying Josephus‑style algorithm, showing each transformation of a slice of cards with complete code snippets, and demonstrating how the final single card matches the hidden one.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Recreating the Spring Festival Gala Magic Trick Using Go

The Spring Festival Gala featured a comedic magic act by Liu Qian, which can be modeled as a deterministic "Josephus problem". This tutorial recreates the entire trick using the Go programming language, focusing on slice manipulation rather than formal mathematics.

Step 1 – Shuffle: Four cards are represented by an int slice and shuffled with rand.Shuffle . Example output: [6 7 5 2] .

// we use an int slice `pokers` to store the cards
var pokers []int = []int{2, 7, 6, 5}

// 1. shuffle randomly
rand.NewSource(time.Now().UnixNano())
rand.Shuffle(len(pokers), func(i, j int) {
    pokers[i], pokers[j] = pokers[j], pokers[i]
})
fmt.Printf("1. 洗牌后的牌:%v\n", pokers)

Step 2 – Double the deck: The slice is duplicated using append(pokers, pokers...) , yielding [6 7 5 2 6 7 5 2] .

pokers = append(pokers, pokers...)
fmt.Printf("2. 对折后的牌:%v\n", pokers)

Step 3 – Rotate by name length: If the magician’s name has two characters, the first two cards are moved to the bottom.

pokers = append(pokers[2:], pokers[:2]...)
fmt.Printf("3. 名字长度旋转:%v\n", pokers)

Step 4 – Insert top three cards into the middle: The first three cards are taken out and inserted before the last element.

pokers = append(pokers[3:7], pokers[0], pokers[1], pokers[2], pokers[7])
fmt.Printf("4. 插入中间:%v\n", pokers)

Step 5 – Hide the top card: The top card is stored in top and removed from the slice.

top := pokers[0]
pokers = pokers[1:]
fmt.Printf("5. 隐藏的牌:%d, %v\n", top, pokers)

Step 6 – Regional variation: Assuming the magician is from the north, two cards are taken from the top and inserted before the last card.

pokers = append(pokers[2:6], pokers[0], pokers[1], pokers[6])
fmt.Printf("6. 北方操作:%v\n", pokers)

Step 7 – Gender variation: For a male magician, one card is removed from the top.

pokers = pokers[1:]
fmt.Printf("7. 男性操作:%v\n", pokers)

Step 8 – "Witness the miracle" loop: Seven iterations move the top card to the bottom, simulating the chant "见证奇迹的时刻".

for range []string{"见","证","奇","迹","的","时","刻"} {
    pokers = append(pokers[1:], pokers[0])
}
fmt.Printf("8. 见证奇迹的时刻:%v\n", pokers)

Step 9 – Final blessing: Five repetitions (male case) move the top card to the bottom then discard the next top card, leaving a single card.

for range []int{1,2,3,4,5} {
    // 好运留下来
    pokers = append(pokers[1:], pokers[0])
    // 烦恼丢出去
    pokers = pokers[1:]
}
fmt.Printf("9. 最终牌:%v\n", pokers)

The final slice contains only [7] , matching the hidden top card, thus "witnessing the miracle":

fmt.Printf("见证奇迹:%d == %d", top, pokers[0])

The article also points out a common mistake made by the assistant host (mis‑ordering the insertion in step 6) and provides the corrected code.

All source code is available on GitHub for readers to experiment with.

algorithmProgrammingGoJosephus problemMagic Trickslice manipulation
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.