I wrote a Player package in Go. It builds correctly, but I would love a review on what I did and did not do "the Go way". I'm new to coding in the language, although I've gotten about halfway through Ivo Balbaert's "The Way To Go".
The code consists of a struct and a bunch of setters and getters for the struct.
package Player
import (
"math/rand"
"time"
)
type Player struct {
name string
race string
id int64
pos Position
facing int64
health int64
wpn_id int64 // Weapon id
is_buf bool // Has buff
buf_mt float64 // Buff multiplier
}
type Position map[int64]int64
func (P *Player) SetPlayer(name string, race string) *Player {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
P.name = name
P.race = race
P.id = rand.Int63()
P.pos = Position{0: 0}
P.facing = 0
P.health = 100
P.wpn_id = 0
P.is_buf = false
P.buf_mt = 0.0
return P
}
func (P *Player) GetPlayer() *Player {
return P
}
func (P *Player) SetPosition(x int64, y int64) {
P.pos = Position{x: y}
}
func (P *Player) GetPosition() Position {
return P.pos
}
func (P *Player) SetFacing(direction int64) {
P.facing = direction
}
func (P *Player) GetFacing() int64 {
return P.facing
}
func (P *Player) SetHealth(amount int64, direction string) {
if direction == "down" {
P.health = P.health - amount
} else {
P.health = P.health + amount
}
}
func (P *Player) GetHealth() int64 {
return P.health
}
func (P *Player) SetWeaponId(weapon_id int64) {
P.wpn_id = weapon_id
}
func (P *Player) GetWeaponId() int64 {
return P.wpn_id
}
func (P *Player) SetIsBuffed(is_buff bool) {
P.is_buf = is_buff
}
func (P *Player) GetIsBuffed() bool {
return P.is_buf
}
func (P *Player) SetBuffMultiplier(multiplier float64) {
P.buf_mt = multiplier
}
func (P *Player) GetBuffMultiplier() float64 {
return P.buf_mt
}