2016-12-21 1 views
1

スライスを作成しようとしていて、リテラルとポインタに問題があります。それは、私のスライスにポインタを渡したという事実が気に入らない場合のように見えます。例えば、私はコンポーネントと呼ばれるパッケージタイプを持ち、コンポーネントタイプを保持しています。下記参照。リテラルとポインタを返す

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

ERRORはCOMPLILE ON HERE起こります。 compSlice:=([] components.Component、5) compSlice =アペンド(compSlice、湧き水、砂)

ERRORが作る:土を使用することはできませんタイプコンポーネントとしてタイプ(* components.Component)としてndを追加します。

セッターとフィールドの設定を直接使用することができますが、これをスライダに追加することはできますが、*ポインタを返すメソッドを使用するとスライスは遵守しません。私は理解していないし、困難な問題があるだけです。私はプログラミングには新しく、基本的にスクリプトから来ているので、これについて私を許して、同様の質問を見ましたが、この文脈では理解しません。

+2

そして、あなたのデザインにコメントここ

は、コメントとコードです。構造体メンバを 'SetName'のような関数の背後に隠したい場合は、それらを小文字にする必要があります。それらをUpperCaseにすると、誰もが直接使用できるようになります。そして私はそれをやっているだろう。なぜなら、機能を隠すことは愚かであると考えるからだが、あなたはGoインターフェースのためにそれを必要としている。 –

+0

OKもう少し詳しく説明して、私が何を思いついたのか、まだ苦労しているのか、皆さんに知らせてください。私はすごくすぐに私に返信してくれる人がいらっしゃいました。みんなありがとう – RedBloodMage

答えて

1

Goは、値とポインタの違いをいくつかの場所で非表示にしますが、すべての場所では表示しません。

このような状況で、あなたは間接参照Sandに持っているとcompSlice = append(compSlice, SpringWater, *Sand)

1

component.Component*component.Componentが行くの異なる種類があります書き込みます。そのスライスをcompSlice := make([]*component.Component, 5)またはappend(compSlice, *SpringWater)にします。

*component.Componentはすべてcomponent.Componentではありません。

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

関連する問題