You could have a cleaner switch statement if you had a list of the keys that were pressed, instead of needing to check each one individually:
func whichKeys(keysToCheck ...keyID) []keyID { var result []keyID for _, k := range keysToCheck { if rl.IsKeyDown(k) { result = append(result, k) } } return result } for _, k := range whichKeys(rl.KeyA, rl.KeyD, rl.KeyW, rl.KeyS) { switch k { case rl.KeyA: p.Rect.X -= 1 case rl.KeyD: p.Rect.X += 1 case rl.KeyW: p.Rect.Y -= 1 case rl.KeyS: p.Rect.Y += 1 } } Andy > On May 1, 2019, at 2:51 PM, Burak Serdar <bser...@ieee.org> wrote: > > On Wed, May 1, 2019 at 2:31 PM <lgod...@gmail.com <mailto:lgod...@gmail.com>> > wrote: >> >> Great example of why future Go updates should include the ternary operator. >> Your code is mess-ey when written using keywords 'if' or 'switch' >> but using '?' it becomes much cleaner >> >> p.Rect.X += rl.IsKeyDown(rl.KeyA) ? -1:0 + (rl.IsKeyDown(rl.KeyD) ? 1 : >> 0 ) >> p.Rect.Y += rl.IsKeyDown(rl.KeyW) ? -1:0 + (rl.IsKeyDown(rl.KeyS) ? 1 : >> 0 ) > > I don't think this is readable at all. I think the cascading-ifs > version is much easier to read. > > You could do something like the following, but I think pointless > unless there's more to the original snippet: > > checkKey:=func(k, v int) int { > if rl.IsKeyDown(k) { > return v > } > return 0 > } > p.Rect.X+=checkKey(rl.KeyA,-1)+checkKey(rl.KeyD,1) > p.Rect.Y+=checkKey(rl.KeyW,-1)+checkKey(,rl.KeyS,1) > >> >> On Wednesday, May 1, 2019 at 8:38:10 AM UTC-4, гусь wrote: >>> >>> if rl.IsKeyDown(rl.KeyA) { >>> p.Rect.X -= 1 >>> } >>> if rl.IsKeyDown(rl.KeyD) { >>> p.Rect.X += 1 >>> } >>> if rl.IsKeyDown(rl.KeyW) { >>> p.Rect.Y -= 1 >>> } >>> if rl.IsKeyDown(rl.KeyS) { >>> p.Rect.Y += 1 >>> } >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts+unsubscr...@googlegroups.com. >> For more options, visit https://groups.google.com/d/optout. > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout > <https://groups.google.com/d/optout>. -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.