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

当前显示为未定义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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从其他目录调用方法,但收到一条错误消息,指出该方法不存在。 我有首字母大写的方法。

我有以下目录结构

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

我的主程序位于hello.go中,并尝试从remote_method程序包

 程序包main 
import 
(
“ remote_method” 
} 
 
func调用函数 main(){
 mm:= remote_method.NewObject()
 mm.MethodCall()
} 
   
 
 

remoteMethod.go具有以下内容< / p>

 包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字符串,param2字符串,param3字符串,param4字符串){
 //要运行的代码
} 
   
 
 

我收到错误

  mm.MethodCall未定义(类型remote_method.DeclaredType没有字段或方法MethodCall)
 <  / code>  
 
 

有人可以帮助我找出为什么该方法不可见或我可以找到任何可能的方法。 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
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题