Pointers 返回文本与指针
正在尝试制作切片,但在文本和指针方面存在问题。它看起来像是当附加到我的切片上时,它不喜欢被递给指针的事实。例如,我创建了一个名为components的包类型,并保存类型Component。见下文Pointers 返回文本与指针,pointers,go,literals,Pointers,Go,Literals,正在尝试制作切片,但在文本和指针方面存在问题。它看起来像是当附加到我的切片上时,它不喜欢被递给指针的事实。例如,我创建了一个名为components的包类型,并保存类型Component。见下文 package components import () type Component struct { Name string Element string Color string } func (c *Component) SetName(name string) { c.
package components
import ()
type Component struct {
Name string
Element string
Color string
}
func (c *Component) SetName(name string) {
c.Name = name
}
func (c *Component) SetElement(element string) {
c.Element = element
}
func (c *Component) SetColor(color string) {
c.Color = color
}
func (c *Component) GetName() string {
return c.Name
}
func (c *Component) GetColor() string {
return c.Color
}
func (c *Component) GetElement() string {
return c.Element
}
func NewComponent(name string, color string, element string) *Component {
c := &Component{}
c.SetName(name)
c.SetColor(color)
c.SetElement(element)
return c
}
现在我正在尝试制作一个切片,将我所有的组件放入其中。我正在制作施法组件,如雪、沙、阳光、泉水等
//Creating SpringWater Component with Setters
SpringWater := components.Component{}
SpringWater.SetName("SpringWater")
SpringWater.SetColor("Blue")
SpringWater.SetElement("Water")
//Creating Sand Component using the Constructor
Sand := components.NewComponent("Sand", "Red", "Earth")
在COMPLILE上发生错误:
compSlice:=make([]组件。组件,5)
compSlice=追加(compSlice、泉水、沙子)
错误:无法使用沙子作为类型(*components.Component)作为类型组件。追加中的组件。
现在使用setter并直接设置字段,我可以将其添加到切片中,但是使用方法它返回*指针,切片将不符合要求。我只是不理解,有一个困难的问题。请记住,我是编程新手,基本上来自脚本编写,因此请原谅我在这方面遇到过类似的问题,但在这方面我并不理解。Go在几个地方隐藏了值和指针之间的差异,但不是在所有地方
在这种情况下,您必须取消引用
Sand
并编写compSlice=append(compSlice,SpringWater,*Sand)
component.component
和*component.component
在Go中是不同的类型。您需要将该切片设置为compSlice:=make([]*component.component,5)
或append(compSlice,*SpringWater)
您定义的所有方法都处理
*component.component
而不是component.component
前面的两个答案都是正确的
在我的回答中,我已将您的代码添加到main
包中,并在main函数中添加了一些注释,以帮助您理解代码中的内容
以下是带有注释的代码:
package main
import (
"fmt"
)
type Component struct {
Name string
Element string
Color string
}
func (c *Component) SetName(name string) {
c.Name = name
}
func (c *Component) SetElement(element string) {
c.Element = element
}
func (c *Component) SetColor(color string) {
c.Color = color
}
func (c *Component) GetName() string {
return c.Name
}
func (c *Component) GetColor() string {
return c.Color
}
func (c *Component) GetElement() string {
return c.Element
}
func NewComponent(name string, color string, element string) *Component {
c := &Component{}
c.SetName(name)
c.SetColor(color)
c.SetElement(element)
return c
}
func main() {
// NewComponent will return a *Component type
// So Sand will we a *Component type
Sand := NewComponent("Sand", "Red", "Earth")
// You can create a slice of []*Component
compSlice := make([]*Component, 5)
// Then append Sand which is a *Component Slice
compSlice = append(compSlice, Sand)
// Result: Will be the reference of Sand which is a reference of Component struct
// [<nil> <nil> <nil> <nil> <nil> 0xc8200121b0]
fmt.Println(compSlice)
// Or, you can create a lice of []Component
newCompSlice := make([]Component, 5)
// Then append the reference of Sand which is *Sand
newCompSlice = append(newCompSlice, *Sand)
// Result: will be the literal Component struct
// [{ } { } { } { } { } {Sand Earth Red}]
fmt.Println(newCompSlice)
}
主程序包
进口(
“fmt”
)
类型组件结构{
名称字符串
元素字符串
色串
}
func(c*组件)集合名(名称字符串){
c、 Name=Name
}
func(c*组件)SetElement(元素字符串){
c、 元素=元素
}
func(c*组件)SetColor(颜色字符串){
c、 颜色=颜色
}
func(c*组件)GetName()字符串{
返回c.名称
}
func(c*组件)GetColor()字符串{
返回c.颜色
}
func(c*组件)GetElement()字符串{
返回c.元素
}
func新组件(名称字符串、颜色字符串、元素字符串)*组件{
c:=&组件{}
c、 集合名(名称)
c、 设置颜色(颜色)
c、 集合元素(元素)
返回c
}
func main(){
//NewcomComponent将返回*组件类型
//因此,我们将使用一种*组件类型
沙:=新成分(“沙”、“红”、“土”)
//您可以创建[]*组件的切片
compSlice:=make([]*组件,5)
//然后添加沙子,这是一个*组件切片
compSlice=append(compSlice,Sand)
//结果:将作为Sand的参考,Sand是组件结构的参考
//[0xc8200121b0]
fmt.Println(组件切片)
//或者,您可以创建[]组件的许可证
newCompSlice:=制造([]组件,5)
//然后附加Sand的引用,即*Sand
newCompSlice=append(newCompSlice,*Sand)
//结果:将是文本组件结构
//[{}{}{}{{}{{}{{沙土红}]
fmt.Println(纽科姆许可证)
}
此外,您还可以在:中运行它,并对您的设计发表评论。如果要将结构成员隐藏在函数后面,如SetName
,则应使其小写。如果你把它们变成大写,那么它们将对每个人公开,以便直接使用。我会这样做,因为我认为函数隐藏是愚蠢的,虽然你需要它去围棋界面,但无论如何。好的,我会弄乱它更多,让大家知道我想出了什么,如果我还在挣扎。我不知道有帮助的人会这么快回复我哇!谢谢各位