dongshijiao6890 2015-02-08 17:49 采纳率: 0%
浏览 58
已采纳

设置方法未设置struct属性Golang

I need help with understanding why this error is being thrown:

I am using a pointer because I want it to update the field.

prog.go:56: cannot use MammalImpl literal (type MammalImpl) as type Mammal in array element: MammalImpl does not implement Mammal (SetName method has pointer receiver) prog.go:57: cannot use MammalImpl literal (type MammalImpl) as type Mammal in array element: MammalImpl does not implement Mammal (SetName method has pointer receiver)

I am not sure why this is unable to set/override the name property as follows.

 package main

import (
    "fmt"
)

type Mammal interface {
    GetID() int
    GetName() string
    SetName(s string)
}

type Human interface {
    Mammal

    GetHairColor() string
}

type MammalImpl struct {
    ID   int
    Name string
}

func (m MammalImpl) GetID() int {
    return m.ID
}

func (m MammalImpl) GetName() string {
    return m.Name
}

func (m *MammalImpl) SetName(s string) {
    m.Name = s
}

type HumanImpl struct {
    MammalImpl
    HairColor string
}

func (h HumanImpl) GetHairColor() string {
    return h.HairColor
}

func Names(ms []Mammal) *[]string {
    names := make([]string, len(ms))
    for i, m := range ms {
        m.SetName("Herbivorous") // This modification is not having any effect and throws and error
        names[i] = m.GetName()
    }
    return &names
}

func main() {
    mammals := []Mammal{
        MammalImpl{1, "Carnivorious"},
        MammalImpl{2, "Ominivorious"},
    }

    numberOfMammalNames := Names(mammals)
    fmt.Println(numberOfMammalNames)
}

Go Playground code is here http://play.golang.org/p/EyJBY3rH23

  • 写回答

1条回答 默认 最新

  • dran0703 2015-02-08 18:19
    关注

    The problem is that you have a method SetName() which has a pointer receiver:

    func (m *MammalImpl) SetName(s string)
    

    So if you have a value of type MammalImpl, the method set of that value does not contain the SetName() method therefore it does not implement the Mammal interface.

    But the method set of a pointer to MammalImpl (*MammalImpl) will contain the SetName() method therefore it will implement the Mammal interface.

    So when you populate the mammals slice, you have to populate it with *MammalImpl values, because that is the one that implements the element type of the slice (which is Mammal). You can easily obtain a pointer to a MammalImpl if you already have a MammalImpl value: use the address & operator to generate a pointer to the value:

    mammals := []Mammal{
        &MammalImpl{1, "Carnivorious"},
        &MammalImpl{2, "Ominivorious"},
    }
    

    Try your modified program on the Go Playground.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分