:) 我知道这看起来像是一个重复的问题,但我找到的每个问题都没有确切地回答我想要做的事情.我是新来的,所以我可能错过了一些小东西.
我正在try 执行类似于this的操作,但对我来说,不同之处在于我需要一个可以访问多个属性的函数,而不是只访问一个属性.我不是在找界面.我希望这个函数代码只有一次,因为它对于每个不同的 struct 都是相同的.
我是新手,但我认为该函数需要接受/使用 struct 的指针,因为我需要更新它们.
示例:
type ModelA struct {
choices []string
cursor int
selected string
name string
}
type ModelB struct {
choices []string
cursor int
selected string
year int
}
func (m ModelA or ModelB) CommonFunction(originalString string, otherArguments ) string {
originalString += question
// Iterate over our choices
for idx, choice := range m.choices {
// Is the cursor pointing at this choice?
cursor := " " // no cursor
if m.cursor == idx {
cursor = ">" // cursor!
}
// Is this choice selected?
checked := " " // not selected
if m.selected == choice {
checked = "x" // selected!
}
// Render the row
originalString += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice)
}
originalString += "\nPress ctrl + q to quit.\n"
return "\n" + originalString + "\n\n"
}
我不确定我想做什么是可能的,但我想从比我更像是Golang专家的人那里知道.