返回Literal vs Pointer

时间:2016-12-21 20:08:17

标签: pointers go literals

尝试制作切片并遇到文字和指针问题。看起来当附加到我的切片上时,它不喜欢被交给指针的事实。例如,我有一个名为components的包类型,它包含Component类型。见下文。

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
}

现在我正试图制作一个切片,将我的所有组件放入我正在制作施法组件,如Snow,Sand,Sunbeam,Springwater等。

    //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")

错误发生在这里:     compSlice:= make([] components.Component,5)     compSlice = append(compSlice,SpringWater,Sand)

错误:不能将Sand作为类型(* components.Component)用作类型components.Component in append。

现在使用Setters和设置字段DIRECT我可以将它添加到切片但是使用它返回*指针的方法并且Slice将不符合。我只是不理解并且遇到困难的问题。请记住,我是编程新手,基本上来自脚本,请原谅我并看到类似的问题,但在这种情况下不理解。

3 个答案:

答案 0 :(得分:1)

Go隐藏了几个地方的值和指针之间的差异,但不是无处不在。

在这种情况下,您必须取消引用Sand并撰写compSlice = append(compSlice, SpringWater, *Sand)

答案 1 :(得分:1)

Go中的

component.Component*component.Component是不同的类型。您希望该切片为compSlice := make([]*component.Component, 5)append(compSlice, *SpringWater)

您定义的方法都涉及*component.Component而不是component.Component

答案 2 :(得分:1)

以前的答案都是正确的。

在我的回答中,我已将您的代码添加到main包中,并在main function中添加了一些注释,以帮助您了解代码中的内容。

以下是带注释的代码:

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)
}

此外,您可以在repl.it

中运行它