# Golang program for implementation of Tower of Hanoi Algorithm

Tower of Hanoi, is a mathematical puzzle which consists of three towers (pegs) and more than one rings. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. Here is source code of the Go Program to implement Tower of Hanoi Algorithm

### Example

```
``````
package main
import "fmt"
type solver interface {
play(int)
}
// towers is example of type satisfying solver interface
type towers struct {
// an empty struct
}
// play is sole method required to implement solver type
func (t *towers) play(n int) {
t.moveN(n, 1, 2, 3)
}
// recursive algorithm
func (t *towers) moveN(n, from, to, via int) {
if n > 0 {
t.moveN(n-1, from, via, to)
t.moveM(from, to)
t.moveN(n-1, via, to, from)
}
}
func (t *towers) moveM(from, to int) {
fmt.Println("Move disk from rod", from, "to rod", to)
}
func main() {
var t solver
t = new(towers) // type towers must satisfy solver interface
t.play(4)
}
```

### Output

```
``````
Move disk from rod 1 to rod 3
Move disk from rod 1 to rod 2
Move disk from rod 3 to rod 2
Move disk from rod 1 to rod 3
Move disk from rod 2 to rod 1
Move disk from rod 2 to rod 3
Move disk from rod 1 to rod 3
Move disk from rod 1 to rod 2
Move disk from rod 3 to rod 2
Move disk from rod 3 to rod 1
Move disk from rod 2 to rod 1
Move disk from rod 3 to rod 2
Move disk from rod 1 to rod 3
Move disk from rod 1 to rod 2
Move disk from rod 3 to rod 2
```

##### Most Helpful This Week

Declaration of a struct type
Program in Go language to Program to Add Two Matrix Using Multi-dimensional Arrays
Golang program for implementation LIFO Stack and FIFO Queue
Exploring Blockchain: Top 15 Real-World Use Cases in 2024
How do you handle HTTP redirects in Go?
GO language program with example of Sort Functions for integer, strings and float64 data type
Go program to find Name Server (NS) record of a domain
Web Application to Get Trending Hashtags Near a Location
3 Different Examples - How State Works in React?
Golang write struct to XML file