doutuosai3504 2017-03-13 18:52
浏览 33
已采纳

col.ToStrings未定义(类型Columns没有字段或方法ToStrings)

I am attempting to create a type that represents a slice of pointers to another type and define a method for it, my code looks similar to this, albeit a bit simplified for the example:

package column

type Column struct {

    name string
}

type Columns []*Column

func (c Column) ToString() string {

    return c.name
}

func (c Columns) ToStrings() []string {

    var strSlice []string

    for _, v := range c {

        strSlice = append(strSlice, v.ToString())
    }

    return strSlice
}

It is then called in a separate file like this:

import (
    c "main/column"
    "strings"
)

type Columns c.Columns

func ToString(col Columns) string {

    return strings.Join(col.ToStrings(), ",
")
}

However when I try to call the method ToStrings() on the exported "Columns" type I get this error:

columns.ToStrings undefined (type Columns has no field or method ToStrings)

It seems the compiler can't find the method ToStrings(). Is there a way around this? Why can't the compiler find the exported method defined for the "Columns" type?

  • 写回答

1条回答 默认 最新

  • douxi2670 2017-03-13 19:09
    关注

    type Columns c.Columns creates an entirely new type, with a new method set. The only reason to do this is to specifically remove the methods of the exiting type.

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

报告相同问题?

悬赏问题

  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥15 看一下OPENMV原理图有没有错误
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常
  • ¥15 Java,消息推送配置
  • ¥15 Java计划序号重编制功能,此功能会对所有序号重新排序,排序后不改变前后置关系。
  • ¥15 关于哈夫曼树应用得到一些问题