Go,第一个 class 个函数和最佳实践

Go, first class functions, and best practices

在过去的几天里,我一直在寻找解决第一个 class 函数问题的最佳方法(将可调用对象分配给某个变量)以及最佳实践的障碍在效率方面。

假设我正在编写一款 Yugioh 纸牌游戏,我希望每张纸牌类型的纸牌至少具有以下属性:

type card struct {
    name string
    text string
}

我一直在思考在哪里(以及如何)对每张卡的单独功能进行编程。我目前确信 first-class 函数的最佳位置是 type card struct 并将新属性创建为 "callable" 就像我在 Python 中那样(Go playground link).

package main

import "fmt"


type card struct {
    name string
    text string
    f interface{}
}

type monsterCard struct {
    card
    attack int
    defense int
}

type buff func(target *monsterCard) // Could be defined in a second file

type swap func(target *monsterCard, value int) // ditto

var increaseAttack buff = func(target *monsterCard)  { // ditto
    target.attack += 100
}

var swichStats swap = func(target *monsterCard, value int) { // ditto
    attack := target.attack
    target.attack = value
    target.defense = attack
}

func main()  {
    m1 := monsterCard{
        card:    card{
            name: "Celtic Guardian",
            f:    increaseAttack,
        },
        attack:  1400,
        defense: 1200,
    }
    m2 := monsterCard{
        card:    card{
            name:     "Dark Magician",
            f:         swichStats,
        },
        attack:  2500,
        defense: 2100,
    }
    var monsters = [2]monsterCard{m1, m2}
    for _, m := range monsters {
        fmt.Println(m)
        switch m.f.(type) {
        case buff:
            m.f.(buff)(&m)
        case swap:
            m.f.(swap)(&m, m.defense)
        default:
            fmt.Printf("%T", m.f)
        }
        fmt.Println(m)
    }
}

我不太擅长高效的代码,我完全理解我可能会提前优化;但是,我将需要对数百张这样的卡片进行编程,如果这些可调用对象存在于全局范围内并且严重依赖类型断言会使程序变慢,那么我将无法重新组织代码。

您是否可以通过我的方法看到任何明显的问题?我是否首先 - class 功能正常,或者是否存在某种我看不到的明显性能问题?任何和所有帮助将不胜感激!

感谢所有帮助我回答这个问题的人。看起来我仍在使用我的脚本语言思维方式来尝试用编译语言解决问题。与其尝试创建最终导致代码更难阅读的接口,我需要更严格地自定义类型定义,并为每种类型的卡片效果实现不同的函数类型。这种严格的执行也将允许具有多种效果的卡牌存在于游戏中。

package main

type card struct {
    name string
    text string
}

type monsterCard struct {
    card
    attack int
    defense int
}

type buffEffect struct {
    monsterCard
    fn buff
}

type setValueEffect struct {
    monsterCard
    fn swap
}

type buff func(target *monsterCard)

type swap func(target *monsterCard, value int)

var increaseAttack buff = func(target *monsterCard)  {
    target.attack += 100
}

var setAttackValue swap = func(target *monsterCard, value int) {
    target.attack = value
}

func main()  {
    m1 := buffEffect{
        monsterCard: monsterCard{
            card:    card{
                name: "Celtic Guardian",
            },
            attack:  1400,
            defense: 1200,
        },
        fn:          increaseAttack,
    }
    m2 := setValueEffect{
        monsterCard: monsterCard{
            card:    card{
                name: "Dark Magician",
            },
            attack:  2500,
            defense: 2100,
        },
        fn:          setAttackValue,
    }
    m1.fn(&m1.monsterCard)
    m2.fn(&m2.monsterCard, 100)
}

您可以在需要时使用普通函数和闭包:

type card struct {
    name string
    text string
    fn   func(*monsterCard)
}

type monsterCard struct {
    card
    attack  int
    defense int
}

func (mc *monsterCard) call() {
    mc.fn(mc)
}

func increaseAttack(mc *monsterCard) {
    mc.attack += 100
}

func switchStats(mc *monsterCard) {
    mc.attack, mc.defense = mc.defense, mc.attack
}


func updateTextAndAttack(text string, attack int) func(mc *monsterCard) {
    return func(mc *monsterCard) {
        mc.text, mc.attack = text, attack
    }
}

https://play.golang.com/p/v_RbObnu7sN

您还可以在需要时使用普通方法和闭包:

type card struct {
    name string
    text string
}

type monsterCard struct {
    card
    attack  int
    defense int
}

func (mc *monsterCard) increaseAttack() {
    mc.attack += 100
}

func (mc *monsterCard) switchStats() {
    mc.attack, mc.defense = mc.defense, mc.attack
}

func (mc *monsterCard) updateTextAndAttack(text string, attack int) func() {
    return func() {
        mc.text, mc.attack = text, attack
    }
}

https://play.golang.com/p/Eo1mm-seldA