http://openmymind.net/Things-I-Wish-Someone-Had-Told-Me-About-Go/
Trying to get my head around Go, still pretty new. I know refs and pointers in C and I can't seem to get it working in Go. I've read a number of articles on the issue, and still not really managing to understand and implement a solution.
Characters have health and atk points.
Chars can Attack().
Combat round calls Attack() on which Character can attack this round.
Intent, when Attack() is called on Character, health is changed on another Character.
Currently Health never changes on Characters.
Can someone give me a concise example of how to change the values on objects around the RIGHT way?
package main
import (
"fmt"
"math/rand"
"time"
)
//Character health + atk of
type Character struct {
Health, Atk int
}
//Attack ... Character can Attack
func (c *Character) Attack(health, atk int) {
health -= atk
}
//CharacterInterface ... methods for characters
type CharacterInterface interface {
Attack(health, atk int)
}
func combatRound(p, e Character) {
whoAtks := rand.Intn(100)
if whoAtks > 30 {
p.Attack(e.Health, p.Atk)
fmt.Println(p.Health)
} else {
e.Attack(p.Health, e.Atk)
fmt.Println(p.Health)
}
}
func main() {
//seed rand generator for the current run
rand.Seed(time.Now().UTC().UnixNano())
p := Character{20, 5}
e := Character{20, 5}
combatRound(p, e)
fmt.Println("Player Health: %d
Enemy Health: %d", p.Health, e.Health)
}