Two slightly different version for what is is worth (not much): checkKey:=func(k int) int { if rl.IsKeyDown(k) { return 1 } return 0 } p.Rect.X+=-checkKey(rl.KeyA)+checkKey(rl.KeyD) p.Rect.Y+=-checkKey(rl.KeyW)+checkKey(,rl.KeyS)
or checkKey:=func(m,p int) (i int) { if rl.IsKeyDown(p) { i++ } if rl.IsKeyDown(m) { i-- } return } p.Rect.X+=-checkKey(rl.KeyA,rl.KeyD) p.Rect.Y+=-checkKey(rl.KeyW,rl.KeyS) On Wednesday, May 1, 2019 at 2:51:50 PM UTC-7, Burak Serdar wrote: > > On Wed, May 1, 2019 at 2:31 PM <lgo...@gmail.com <javascript:>> 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 golan...@googlegroups.com <javascript:>. > > 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. For more options, visit https://groups.google.com/d/optout.