dongxikuo5171 2019-03-26 01:42
浏览 145
已采纳

当前显示为未定义golang的方法

I am trying to call a method from a different directory but getting an error saying that the method is not present. I have the method present with the first letter Uppercase.

I have the following directory structure

[laptop@laptop src]$ tree
.
├── hello
│   ├── hello.go
├── remote_method
│   └── remoteMethod.go

My main is in hello.go and tries to call a function from the remote_method package

package main
import 
 (
        "remote_method"
 )

func main() {
     mm := remote_method.NewObject()
     mm.MethodCall()
}

The remoteMethod.go has the following contents

package remote_method

import (
.....
)

type DeclaredType struct {
        randomMap (map[string][](chan int))
}

func NewObject() DeclaredType {
        var randomMap (map[string][](chan int))
        m := DeclaredType{randomMap}
        return m
}

func MethodCall(m DeclaredType, param1 string, param2 string, param3 string, param4 string) {
     // Code to be run
}

I get the error

mm.MethodCall undefined (type remote_method.DeclaredType has no field or method MethodCall)

Can someone help me in finding why the method is not visible or any possible ways I could find that. TIA

  • 写回答

1条回答 默认 最新

  • doulue1949 2019-03-26 02:07
    关注

    Register MethodCall() as a receiver in DeclaredType.

    remote_method.go

    package remote_method
    
    import (
    .....
    )
    
    type DeclaredType struct {
            randomMap (map[string][](chan int))
    }
    
    func NewObject() DeclaredType {
            var randomMap (map[string][](chan int))
            m := DeclaredType{randomMap}
            return m
    }
    
    func (d DeclaredType) MethodCall(m DeclaredType, param1 string, param2 string, param3 string, param4 string) {
         // Code to be run
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘