Receiver function not being found in a Go application [closed]

Solution for Receiver function not being found in a Go application [closed]
is Given Below:

I am struggling to work out why a receiver function in a go application isn’t being found.

I’m comparing my code to other tutorial code as for the life of me can’t see the problem.

I have a struct in main.go called application and am creating an instance of it in the main function.

type application struct {
    cnf    cnf
    logger *log.Logger
}

....

func main {
...
app := &application{
        cnf:    cfg,
        logger: logger,
    }
...

In the same main.go file I have other functions that are function receivers that seem to be working fine, but I have one function in a separate file, in package main with the signature

func (app *application) sendSlackMessage(msg string) error {}

but, when I try and call it from another function in main, like so

err := app.sendSlackMessage(fmt.Sprintf("MESSAGE"))

I get an error

app.sendSlackMessage undefined (type *application has no field or method SendSlackMessage)

I’m not sure if the issue is that its in another file (although the tutorial code does that) or what the issues is.

Running go run main.go will compile and execute only the main.go file. Any other .go files in that package will be left out.

Instead try running one of:

  • go run .
  • go run *.go
  • go run my/cmd
  • go build -o ./main && ./main

https://pkg.go.dev/cmd/go#hdr-Compile_and_run_Go_program

Run compiles and runs the named main Go package. Typically the package
is specified as a list of .go source files from a single directory,
but it may also be an import path, file system path, or pattern
matching a single known package
, as in ‘go run .’ or ‘go run my/cmd’.