From 40713cc5c003a5ebde9a8a832efc9f0389513aad Mon Sep 17 00:00:00 2001 From: n9te9 Date: Mon, 20 Apr 2026 20:23:18 +0900 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20gocon2026=20=E7=94=A8=E3=81=AB?= =?UTF-8?q?=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/lottery/main.go | 21 +++++----- lottery.go | 30 ++++++-------- lottery_test.go | 12 ++++-- plan.go | 40 ++++++++----------- testdata/TestLotteryResult_Show/normal.golden | 12 ++++-- testdata/practice.csv | 36 ++++++++--------- 6 files changed, 76 insertions(+), 75 deletions(-) diff --git a/cmd/lottery/main.go b/cmd/lottery/main.go index eb08cf9..c183955 100644 --- a/cmd/lottery/main.go +++ b/cmd/lottery/main.go @@ -9,15 +9,17 @@ import ( ) var ( - flagPlaTinumCount int - flagGoldCount int - flagSilverCount int + flagGoldCount int + flagSilverCount int + flagLunchCount int + flagDrinkCount int ) func init() { - flag.IntVar(&flagPlaTinumCount, "p", 2, "counts of platinum plan") - flag.IntVar(&flagGoldCount, "g", 2, "counts of gold plan") - flag.IntVar(&flagSilverCount, "s", 18, "counts of silver plan") + flag.IntVar(&flagGoldCount, "g", 6, "counts of gold plan") + flag.IntVar(&flagSilverCount, "s", 12, "counts of silver plan") + flag.IntVar(&flagLunchCount, "l", 2, "counts of lunch plan") + flag.IntVar(&flagDrinkCount, "d", 2, "counts of drink plan") } func main() { @@ -35,9 +37,10 @@ func run() error { } l := &sponsors.Lottery{ - PlaTinumCount: flagPlaTinumCount, - GoldCount: flagGoldCount, - SilverCount: flagSilverCount, + GoldCount: flagGoldCount, + SilverCount: flagSilverCount, + LunchCount: flagLunchCount, + DrinkCount: flagDrinkCount, } result := l.Do(applicants) diff --git a/lottery.go b/lottery.go index 3feda2c..abc0fbd 100644 --- a/lottery.go +++ b/lottery.go @@ -9,19 +9,22 @@ import ( ) type Lottery struct { - PlaTinumCount int - GoldCount int - SilverCount int + GoldCount int + SilverCount int + LunchCount int + DrinkCount int } func (l *Lottery) Limit(p Plan) int { switch p { - case PlanPlaTinum: - return l.PlaTinumCount case PlanGold: return l.GoldCount case PlanSilver: return l.SilverCount + case PlanLunch: + return l.LunchCount + case PlanDrink: + return l.DrinkCount } return -1 } @@ -31,8 +34,7 @@ func (l *Lottery) Do(applicants map[Plan][]*Applicant) LotteryResult { doLotteries := make([]func() (Plan, []*Applicant), 0, len(plans)) for _, plan := range plans { doLotteries = append(doLotteries, func() (Plan, []*Applicant) { - sponsors, nexts := l.doPlan(applicants[plan], l.Limit(plan)) - applicants[plan.Next()] = append(applicants[plan.Next()], nexts...) + sponsors := l.doPlan(applicants[plan], l.Limit(plan)) return plan, sponsors }) } @@ -46,7 +48,7 @@ func (l *Lottery) Do(applicants map[Plan][]*Applicant) LotteryResult { return result } -func (l *Lottery) doPlan(as []*Applicant, n int) (sponsors, nexts []*Applicant) { +func (l *Lottery) doPlan(as []*Applicant, n int) (sponsors []*Applicant) { rand.Shuffle(len(as), func(i, j int) { as[i], as[j] = as[j], as[i] }) @@ -59,16 +61,10 @@ func (l *Lottery) doPlan(as []*Applicant, n int) (sponsors, nexts []*Applicant) sponsors = slices.Clone(as[:n]) if len(as)-n <= 0 { - return sponsors, nil + return sponsors } - for _, a := range as[n:] { - if a.Next { - nexts = append(nexts, a) - } - } - - return sponsors, nexts + return sponsors } type LotteryResult map[Plan][]*Applicant @@ -89,7 +85,7 @@ func (r LotteryResult) Show(w io.Writer) { func (r LotteryResult) PlanDelay(p Plan) time.Duration { switch p { - case PlanPlaTinum: + case PlanLunch: return 1 * time.Second case PlanGold: return 1 * time.Second diff --git a/lottery_test.go b/lottery_test.go index 47bd371..5f5ae14 100644 --- a/lottery_test.go +++ b/lottery_test.go @@ -27,10 +27,6 @@ func TestLotteryResult_Show(t *testing.T) { result sponsors.LotteryResult }{ "normal": {result: sponsors.LotteryResult{ - sponsors.PlanPlaTinum: []*sponsors.Applicant{ - {Name: "PlaTinum 01", Plan: sponsors.PlanPlaTinum, Next: true}, - {Name: "PlaTinum 02", Plan: sponsors.PlanPlaTinum, Next: true}, - }, sponsors.PlanGold: []*sponsors.Applicant{ {Name: "GoldTinum 01", Plan: sponsors.PlanGold, Next: true}, {Name: "GoldTinum 02", Plan: sponsors.PlanGold, Next: true}, @@ -41,6 +37,14 @@ func TestLotteryResult_Show(t *testing.T) { {Name: "SilverTinum 02", Plan: sponsors.PlanSilver, Next: true}, {Name: "SilverTinum 03", Plan: sponsors.PlanSilver, Next: true}, }, + sponsors.PlanLunch: []*sponsors.Applicant{ + {Name: "LunchTinum 01", Plan: sponsors.PlanLunch, Next: true}, + {Name: "LunchTinum 02", Plan: sponsors.PlanLunch, Next: true}, + }, + sponsors.PlanDrink: []*sponsors.Applicant{ + {Name: "DrinkTinum 01", Plan: sponsors.PlanDrink, Next: true}, + {Name: "DrinkTinum 02", Plan: sponsors.PlanDrink, Next: true}, + }, }}, } diff --git a/plan.go b/plan.go index fc51c9c..83046bc 100644 --- a/plan.go +++ b/plan.go @@ -4,25 +4,23 @@ type Plan string func Plans() []Plan { return []Plan{ - PlanPlaTinum, PlanGold, PlanSilver, - PlanBronze, - PlanFree, + PlanLunch, + PlanDrink, } } const ( - PlanPlaTinum Plan = "platinum" - PlanGold Plan = "gold" - PlanSilver Plan = "silver" - PlanBronze Plan = "bronze" - PlanFree Plan = "free" + PlanGold Plan = "gold" + PlanSilver Plan = "silver" + PlanLunch Plan = "lunch" + PlanDrink Plan = "drink" ) func (p Plan) IsLottery() bool { switch p { - case PlanPlaTinum, PlanGold, PlanSilver: + case PlanGold, PlanSilver, PlanLunch, PlanDrink: return true default: return false @@ -31,32 +29,28 @@ func (p Plan) IsLottery() bool { func (p Plan) Title() string { switch p { - case PlanPlaTinum: - return `Platinum "Go"ld` case PlanGold: return `"Go"ld` case PlanSilver: return "Silver" - case PlanBronze: - return "Bronze" - case PlanFree: - return "Free" + case PlanLunch: + return "Lunch" + case PlanDrink: + return "Drink" + default: + return "unknown plan" } - - return "unknown plan" } func (p Plan) Next() Plan { switch p { - case PlanPlaTinum: - return PlanGold case PlanGold: return PlanSilver case PlanSilver: - return PlanBronze - case PlanBronze: - return PlanBronze + return PlanLunch + case PlanLunch: + return PlanDrink default: - return PlanFree + return "" } } diff --git a/testdata/TestLotteryResult_Show/normal.golden b/testdata/TestLotteryResult_Show/normal.golden index 0d9ec3f..be712f7 100644 --- a/testdata/TestLotteryResult_Show/normal.golden +++ b/testdata/TestLotteryResult_Show/normal.golden @@ -1,7 +1,3 @@ -==== Platinum "Go"ld sponsor ==== -PlaTinum 01 -PlaTinum 02 - ==== "Go"ld sponsor ==== GoldTinum 01 GoldTinum 02 @@ -12,3 +8,11 @@ SilverTinum 01 SilverTinum 02 SilverTinum 03 +==== Lunch sponsor ==== +LunchTinum 01 +LunchTinum 02 + +==== Drink sponsor ==== +DrinkTinum 01 +DrinkTinum 02 + diff --git a/testdata/practice.csv b/testdata/practice.csv index d7fae5d..6c5fe05 100644 --- a/testdata/practice.csv +++ b/testdata/practice.csv @@ -1,38 +1,38 @@ company,plan,next 株式会社Gopher1,free,FALSE -株式会社Gopher2,platinum,TRUE +株式会社Gopher2,lunch,TRUE 株式会社Gopher3,gold,TRUE 株式会社Gopher4,silver,TRUE -株式会社Gopher5,bronze,FALSE +株式会社Gopher5,drink,FALSE 株式会社Gopher6,gold,TRUE 株式会社Gopher7,gold,TRUE 株式会社Gopher8,gold,TRUE -株式会社Gopher9,bronze,TRUE +株式会社Gopher9,drink,TRUE 株式会社Gopher10,gold,TRUE -株式会社Gopher11,bronze,FALSE +株式会社Gopher11,drink,FALSE 株式会社Gopher12,gold,TRUE 株式会社Gopher13,gold,TRUE -株式会社Gopher14,platinum,TRUE -株式会社Gopher15,platinum,TRUE +株式会社Gopher14,lunch,TRUE +株式会社Gopher15,lunch,TRUE 株式会社Gopher16,silver,TRUE -株式会社Gopher17,bronze,FALSE -株式会社Gopher18,platinum,TRUE -株式会社Gopher19,bronze,FALSE -株式会社Gopher20,bronze,TRUE -株式会社Gopher21,platinum,TRUE +株式会社Gopher17,drink,FALSE +株式会社Gopher18,lunch,TRUE +株式会社Gopher19,drink,FALSE +株式会社Gopher20,drink,TRUE +株式会社Gopher21,lunch,TRUE 株式会社Gopher22,silver,TRUE 株式会社Gopher23,gold,TRUE -株式会社Gopher24,platinum,TRUE +株式会社Gopher24,lunch,TRUE 株式会社Gopher25,silver,TRUE 株式会社Gopher26,silver,FALSE 株式会社Gopher27,gold,TRUE -株式会社Gopher28,platinum,TRUE +株式会社Gopher28,lunch,TRUE 株式会社Gopher29,gold,TRUE 株式会社Gopher30,silver,TRUE 株式会社Gopher31,gold,TRUE -株式会社Gopher32,platinum,TRUE +株式会社Gopher32,lunch,TRUE 株式会社Gopher33,gold,TRUE -株式会社Gopher34,platinum,FALSE +株式会社Gopher34,lunch,FALSE 株式会社Gopher35,gold,TRUE 株式会社Gopher36,silver,TRUE 株式会社Gopher37,gold,TRUE @@ -40,7 +40,7 @@ company,plan,next 株式会社Gopher39,gold,TRUE 株式会社Gopher40,gold,TRUE 株式会社Gopher41,gold,TRUE -株式会社Gopher42,bronze,FALSE +株式会社Gopher42,drink,FALSE 株式会社Gopher43,gold,TRUE 株式会社Gopher44,gold,TRUE 株式会社Gopher45,gold,TRUE @@ -50,5 +50,5 @@ company,plan,next 株式会社Gopher49,silver,TRUE 株式会社Gopher50,silver,TRUE 株式会社Gopher51,silver,TRUE -株式会社Gopher52,bronze,TRUE -株式会社Gopher53,platinum,TRUE \ No newline at end of file +株式会社Gopher52,drink,TRUE +株式会社Gopher53,lunch,TRUE \ No newline at end of file From c892dfdf14c6417dcc2a3a2c774174bc607fc109 Mon Sep 17 00:00:00 2001 From: n9te9 Date: Mon, 20 Apr 2026 20:30:57 +0900 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20error=20=E3=83=8F=E3=83=B3=E3=83=89?= =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=97?= =?UTF-8?q?=E3=81=BE=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 5 +++-- lottery.go | 7 ++++++- plan.go | 30 +++++++++++++++++++----------- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 1a7d2c4..26811c3 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,12 @@ module github.com/GoCon/sponsors -go 1.25rc1 +go 1.26.2 + +require github.com/tenntenn/golden v0.5.5 require ( github.com/google/go-cmp v0.7.0 // indirect github.com/josharian/mapfs v0.0.0-20210615234106-095c008854e6 // indirect github.com/josharian/txtarfs v0.0.0-20240408113805-5dc76b8fe6bf // indirect - github.com/tenntenn/golden v0.5.5 // indirect golang.org/x/tools v0.33.0 // indirect ) diff --git a/lottery.go b/lottery.go index abc0fbd..76b0981 100644 --- a/lottery.go +++ b/lottery.go @@ -1,6 +1,7 @@ package sponsors import ( + "errors" "fmt" "io" "math/rand/v2" @@ -74,7 +75,11 @@ func (r LotteryResult) Show(w io.Writer) { if !plan.IsLottery() { continue } - fmt.Fprintf(w, "==== %s sponsor ====\n", plan.Title()) + title, err := plan.Title() + if err, ok := errors.AsType[ErrUnknownPlan](err); ok { + title = err.Error() + } + fmt.Fprintf(w, "==== %s sponsor ====\n", title) for _, applicant := range r[plan] { r.printApplicant(w, applicant.Name, r.PlanDelay(plan)) } diff --git a/plan.go b/plan.go index 83046bc..683cb55 100644 --- a/plan.go +++ b/plan.go @@ -1,5 +1,7 @@ package sponsors +import "fmt" + type Plan string func Plans() []Plan { @@ -27,30 +29,36 @@ func (p Plan) IsLottery() bool { } } -func (p Plan) Title() string { +type ErrUnknownPlan struct{} + +func (e ErrUnknownPlan) Error() string { + return "unknown plan" +} + +func (p Plan) Title() (string, error) { switch p { case PlanGold: - return `"Go"ld` + return `"Go"ld`, nil case PlanSilver: - return "Silver" + return "Silver", nil case PlanLunch: - return "Lunch" + return "Lunch", nil case PlanDrink: - return "Drink" + return "Drink", nil default: - return "unknown plan" + return "unknown plan", fmt.Errorf("unknown plan: %s", p) } } -func (p Plan) Next() Plan { +func (p Plan) Next() (Plan, error) { switch p { case PlanGold: - return PlanSilver + return PlanSilver, nil case PlanSilver: - return PlanLunch + return PlanLunch, nil case PlanLunch: - return PlanDrink + return PlanDrink, nil default: - return "" + return "", fmt.Errorf("unknown plan: %s", p) } }