Can not place function in package

I faced a problem when for some reason I can not place function in package and then use that package. Look at the pic below:

Untitled
Here is a main.go file which is in package main and I moved function randomInt into package cityutil and try to use it in the main.go file. So IDE suggest to me to import that newly created package - I did it. Look at it:

1Untitled

Then I press save file and those import “citiestemp/cityutil” disapeared for some reason. So I mention what is the reason of that behaviour? Why I cat not import it that way?

1 Like

Solution - the reason was a lovercase function name.

But there is other question. Why I can not import package main? When I try to import main package into cityutil package, then I get from Ide:

“import “citiestemp/main” is a program, not an importable packagego”

So package main is unimportable?

2 Likes

I’ve found a discussion https://groups.google.com/forum/#!msg/golang-nuts/frh9zQPEjUk/Ev6FJze8BwAJ and it seems that in general for my case main package is unimportable.

1 Like

@cinematik from my understanding; the functions and methods declared in main.go can not be exported to packages.

In order to use a function/method across your application you would have to create a package that exports those functions/methods and then import them to each script.

2 Likes

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