我怎么能让这个 go 程序递归。我正在通过编写游戏编号分析器来学习 golang。我一直在思考如何做到这一点,但无法提出一个可行的解决方案。这是Google Playground 中的链接。任何帮助将不胜感激。
/*
File record.go
Author: Dan Huckson
Date: 20160120
Purpose: Number analyzer
*/
package main
import (
"fmt"
)
type Stats struct {
category map[string]Events
}
type Events struct {
event map[string]*Event
}
type Event struct {
value int64
}
func main() {
winners := [][]int{
{1, 2, 3, 4, 5, 6},
{2, 4, 6, 28, 26, 39},
{1, 4, 9, 10, 26, 39},
{1, 9, 19, 29, 26, 49},
{4, 5, 6, 28, 26, 49}}
keys := []string{"digits1", "digits2", "digits3", "digits4", "digits5", "digits6"}
stats := new(Stats)
stats.category = make(map[string]Events)
for _, key := range keys {
events, ok := stats.category[key]
if !ok {
events = *new(Events)
}
events.event = make(map[string]*Event)
stats.category[key] = events
}
fmt.Println()
for _, winner := range winners {
fmt.Println(winner)
stats.digits1("digits1", winner)
stats.digits2("digits2", winner)
stats.digits3("digits3", winner)
stats.digits4("digits4", winner)
stats.digits5("digits5", winner)
stats.digits6("digits6", winner)
}
}
func (stats *Stats) record(key string, balls string) {
event, ok := stats.category[key].event[balls]
if !ok {
event = new(Event)
stats.category[key].event[balls] = event
}
stats.category[key].event[balls].value += 1
word := ""
if len(balls) > 1 {
word = "Balls"
} else {
word = "Ball"
}
fmt.Printf("%s:%s\tCount:%d\n", word, balls_to_csv(balls), stats.category[key].event[balls].value)
}
func (stats *Stats) digits1(key string, winner []int) {
for i := 0; i < len(winner); i++ {
stats.record(key, string(winner[i]))
}
}
慕后森
湖上湖
相关分类