The_Lost_Avatar
The_Lost_Avatar

Reputation: 990

Method that is present being shown as undefined 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

Upvotes: 0

Views: 1509

Answers (1)

sh.seo
sh.seo

Reputation: 1610

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
}

Upvotes: 7

Related Questions