Not able to import functions

I am facing issue while importing function. I have the directory structure as mentioned in the screenshot.
I want to import function “handlerReadiness” from handler_readiness.go into main.go. But when I run the main.go file, it says “undefined handlerReadiness”.
Although when I hover over handlerReadiness, I can see the function in defined and can reach to it’s definition by doing command+click.

Please help in resolving this issue.

View the handler_readiness.go file.

Lowercase names mean “private”, uppercase names “public”, so you need to rename the function to something with an uppercase letter at the beginning.

You also want to use another package for that, which you can then import from your main.

Within the same package it doesn’t matter.

Hi @mj4code, the file handler_readiness.go resides in the same directory as main.go, hence it must also belong to package main. You do not need to import functions from another file of the same package. They are available to all other files of that package.

If you want to make handler_readiness.go a separate package, move it to a subdirectory and ensure the functions are exported (as @NobbZ has described).

I think you need to use: go run .
instead of go run main.go which will only compile one file and not all go files in the directory.

The go devs even suggest you should use go build && go-project.exe to run your code instead of go run in development.


It isn’t the same package. And if it is, then that should really be changed.

I’ve had nothing but trouble using a main package spanning multiple files.

Good catch.

1 Like

@falco467 Yeah that worked. Thanks alot !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.