Reputation: 10829
my project organisation looks like this :
main.go looks like this :
package main
import (
"fmt"
"cvs/user/project/utils"
)
func main() {
...
utilsDoSomething()
...
}
and utils.go :
package utils
import (
"fmt"
)
func utilsDoSomething() {
...
}
The compiler tells me that :
main.go imported and not used: "cvs/user/project/utils"
main.go undefined: utilsDoSomething
I don't know what I'm doing wrong. Any idea would be helpful, thank you in advance !
Upvotes: 6
Views: 5691
Reputation: 2943
function name also needs to be capitalized in the package utils
func UtilsDoSomething()
Upvotes: 0
Reputation: 49181
In Go the symbols (including function names) starting with lower case letters are not exported and so are not visible to other packages.
Rename the function to UtilsDoSomething
.
If you strongly oppose exporting this function and you are using Go 1.5 or later, you can make your function visible to only your project by placing utils
directory inside internal
directory.
Specification of internal packages
Upvotes: 1
Reputation: 11551
When you are referencing a function from a package you need to reference it using the package name.Function name
(with capital case).
In your case use it as:
utils.UtilsDoSomething()
.
Upvotes: 1
Reputation: 57639
You forgot the package prefix in main.go
and your function is not exported, meaning it is not accessible from other packages. To export an identifier, use a capital letter at the beginning of the name:
utils.UtilsDoSomething()
Once you have the utils
prefix you can also drop the Utils
in the name:
utils.DoSomething()
If you want to import everything from the utils
package into the namespace of your main application do:
import . "cvs/user/project/utils"
After that you can use DoSomething
directly.
Upvotes: 11