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 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c