duanfei1987 2016-05-04 08:43
浏览 56
已采纳

如何将结构数组传递给我的结构函数

So, i have my slice renamed for using in functions, which one connected to array (like a class methods).

type Points []Point


func (p Points) Isset(ip Point) bool {
     for _, i := range p {
         if i.Hash == ip.Hash {
             return true
         }
     }
     return false
}

But it's doesn't matter, coz in another function, which one tried to pass slice with type Points, i have some trouble...

Here is example:

func (p Points) Merge(ip Points) {
    fmt.Println(p)
}

In first function - i can access to my p variable as array. In second - p - just empty array. But if i change type of passed variable - everything will be fine.

What should i do... I need to specify me merge function. And this solution look's like awesome, but doesn't work.

  • 写回答

2条回答 默认 最新

  • dongzou7134 2016-05-04 09:04
    关注

    I am not sure I understand, this is a play example showing both functions working as expected as far as I can tell.

    https://play.golang.org/p/n5ch-Wqbil

    Perhaps you are running into some problem calling one function from the other (like calling Isset from Merge maybe?) In that case @Games' answer will probably still apply.

    EDIT this example show what I think you are probably trying to do. https://play.golang.org/p/CNEt-poKdN and it seems to work just fine (though has N^2 time performance)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突